Countdown in Cocos2d

蓝咒 提交于 2019-12-13 04:23:36

问题


I am trying to think through how to implement a countdown on a stamina level. For example, maximum 100% and over time it decreases. I don't need to know how to display it but the technology behind it to get it working and where to look.

This countdown would keep on going down when the application is closed.

I was looking at NSUserDefaults as a means to do this. Is this correct?

Similar to the top bar as shown below:


回答1:


You can save latest refueled stamina value in NSUserDefaults or in iCloud and calculate current value using something like this:

timePassedInSeconds = currentTime - latestMaxValueSaveTime;  
newStamina = latestMaxValue - (timePassedInSeconds * decreasePerSecond);

This way each time the player refuels stamina (e.g. buys some food for animal) you reset stamina to 100% (or add some percentage depending on food type) and save this value into latestMaxValue and save the time it was refueled into latestMaxValueSaveTime (you can store both in NSUserDefaults)

And you calculate newStamina in update: of the scene or in onEnter: method of the scene if it needs to be calculated once.

This way it will decrease even when the app is closed.

However, if you want to avoid players resetting stamina by changing device time you should get time from the server (preferably in UTC, to avoid issues with timezones and daylight saving).



来源:https://stackoverflow.com/questions/24040226/countdown-in-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!