Copy link to clipboard
Copied
I have tried to write codes to my game so that it replenishes the player's lives over time. But I have had no success. For example, in Candy Crush, it's called 'lives'. You can have maximum of 5 lives. And once you start a level, it takes off 1 life from the 5 lives. And then the timer next to the lives label starts counting down from 20 minutes to 0. once it reaches 0, it gives you 1 life back so that you can start a level again. I tried to do this like this: Frame EventListener to count down from 20 minutes to 0 whenever the number of lives is under 5, and once it reaches 5, stop counting down numbers. It sounds simple and it is simple. But what if the player quits the game while the timer is counting down?? Let's say the player left the game at 3:10pm with 3 lives and 10 min left for a life replenish. And the player starts the game again at 3:30pm. Then the player would have 4 lives and 10 min left for a life replenish. I tried to accomplish this by using date class. But the remaining time gets messed up whenever I quit the game and restart the game.
Instead of counting down from an absolute time, count UP until you hit a maximum and dispatch an event when the maximum is reached. Have a separate object that stores the count and can be initialized at any point. When the user quits, store the old count. When they start again, initialize a new instance of the count object with the old count. Display the counting down, if necessary, by subtracting the current value from the maximum.
So your object might look like this:
...
public class Counter extends
Copy link to clipboard
Copied
are you trying to detect when a player is idle? eg, when the mouse fails to move for some period of time? if yes, use a timer that resets on mousemove.
otherwise, what determines when a player leaves the game?
Copy link to clipboard
Copied
the game is for android smartphones. a player can quit the game by pushing the back button of the smartphones or by going deactivated(pushing the middle button to go to the android menu screen or shut down button).
Copy link to clipboard
Copied
use the deactivate event, flash.events.Event.DEACTIVATE
Copy link to clipboard
Copied
Instead of counting down from an absolute time, count UP until you hit a maximum and dispatch an event when the maximum is reached. Have a separate object that stores the count and can be initialized at any point. When the user quits, store the old count. When they start again, initialize a new instance of the count object with the old count. Display the counting down, if necessary, by subtracting the current value from the maximum.
So your object might look like this:
public class Counter extends EventDispatcher {
protected var _count:int;
protected var _max:int;
protected var timer:Timer = new Timer (1000);
public function Counter(count:int=0, max:int=20):void {
_count = count;
_max = max;
}
public function start():void {
timer.addEventListener(TimerEvent.TIMER, incrementTime);
timer.start();
}
public function stop() {
timer.removeEventListener(TimerEvent.TIMER, incrementTime);
timer.stop();
}
public function incrementTime(e:Event=Null):void {
_count++;
dispatchEvent(new Event('countChanged'));
if (_count==_max) {
dispatchEvent(new Event('countIsMax'));
//optionally could reset _count here
}
}
public function get count():int {
return _count;
}
public function get max():int {
return _max;
}
public function get countDown():int {
return _max - _count;
}
}
Usage would look something like:
public class YourGame extends Sprite {
protected var counter:Counter;
protected var lives:int;
public var countText:TextField;
public var livesText:TextField;
public function YourGame() {
super();
//logic to retrieve stored count herecounter = new Counter(storedCountThatYouRetrieved);
counter.addEventListener('countChanged', updateCountText);
counter.addEventListener('countIsMax', updateLives);
}
protected function updateCountText(e:Event):void {
countText.text = String(counter.countDown);
}
protected function updateLives(e:Event):void {
lives++;
livesText.text = String(lives);
}
}
Copy link to clipboard
Copied
Thank you it worked out great!
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now