1

I tried many of code so far pause and play to game but did not work many of them.

I got a moving object on stage also I have timer dynamic text.

I need a working pause and play code on my project.

Example my moving object

/*Fish 3 move*/
var balik3x:Number=7;
var balik3y:Number=Math.random()*15
 
stage.addEventListener(Event.ENTER_FRAME,h3);
function h3(oly:Event) {
balik3.x+=balik3x;
balik3.y+=balik3y;
if ((balik3.x>=stage.stageWidth-balik3.width/2)|| (balik3.x <= balik3.width/2 )) {
    balik3x*=-12;
}
if ((balik3.y>=stage.stageHeight-balik3.height/2)|| (balik3.y <= balik3.height/2 )) {
    balik3y*=-1;
}
}
balik3.mouseEnabled = false;

My timer code

UPDATE: @www0z0k Did you mean like this.The timer pause and mouse over bug not exist anymore.But timer is not resume when I click to play button.

time.text="0:10";
var dispSecs=09;
var dispMins=0;
 
var timerInterval=setInterval(countDown,1000);
var control:Timer = new Timer(1000,0)
control.addEventListener(TimerEvent.TIMER, keko)
control.start();
 
function keko (evt:Event):void{

if(dispMins <1 && dispSecs <1 )
{
timeisup.visible = true;
timeisup.play();
}
}
 
function countDown()
{
dispSecs--;
if (dispMins == 0 && dispSecs == 0)
{
clearInterval(timerInterval);
}
else if (dispSecs == 0)
{
dispSecs = 59;
if (dispMins > 0)
{
dispMins--;
}
}
time.text = prependZero(dispMins) + ":" + prependZero(dispSecs);
}  
 
function prependZero(num)
{
if(num<10)
{
num=""+num;
}
return(num);
} 

The last code I tried.But it's not work exactly. UPDATED

play1.addEventListener(MouseEvent.CLICK, resumeGame);
function resumeGame(event:MouseEvent):void{
addEventListener(TimerEvent.TIMER, keko);
stage.frameRate = 30;
}
pause1.addEventListener(MouseEvent.CLICK, pauseGame);
function pauseGame(event:MouseEvent):void{
stage.removeEventListener(TimerEvent.TIMER, keko);
clearInterval(timerInterval);
stage.frameRate = 0.01;

}
KucuKeko
  • 111
  • 8
  • Frame rate cannot be set below 0.01 (according to documentation) and by doing so you get a **horrible** response time of 100 seconds because script (and event handling) runs between frames. – Organis Jan 26 '17 at 08:02
  • I change to pause frameRate = 0.01; and play frameRate = 30; this work for moving object so far. ....Timer pause work fine when I click pause button.But there is an interesting thing happen to its keep running when I mouse over anywhere on stage.I did not click play button but its keep running anyway. – KucuKeko Jan 26 '17 at 08:35
  • I'd suggest clearing the interval on pause and removing the enter frame listener - and re-adding on resume. Another option is to create a global boolean flag, call it 'paused' and check it everywhere you need – www0z0k Jan 26 '17 at 09:51
  • I update my message.Can you check please ? I'm not sure how I can fix that. – KucuKeko Jan 26 '17 at 10:47
  • Do you know what an `ENTER_FRAME` function is used for & why it works like that? When you know the simple answer about how your framerate and movement code are connected by `addEventListener(Event.ENTER_FRAME,h3)` then maybe you'll understand how to do a simple pause. Also you should make a `pause` function that stops any time/updates related code from running. – VC.One Jan 26 '17 at 19:36

2 Answers2

2

Not specific to your code but you'll get the idea

private function togglePause():void{
    // check if the event listener exists
    if (!stage.hasEventListener(Event.ENTER_FRAME)){
    // add it if it doesn't 
        stage.addEventListener(Event.ENTER_FRAME, tick);
    } else {
    // remove it if it does
        stage.removeEventListener(Event.ENTER_FRAME, tick);
    }
}
Neal Davis
  • 2,010
  • 2
  • 12
  • 25
1

I think there might be a small mistake , you have added the event listener to this and try to remove the event listener from stage. try this code and if this doesnt work let me know I will provide you another solution

play1.addEventListener(MouseEvent.CLICK, resumeGame);
function resumeGame(event:MouseEvent):void{
addEventListener(TimerEvent.TIMER, keko);
stage.frameRate = 30;
}
pause1.addEventListener(MouseEvent.CLICK, pauseGame);
function pauseGame(event:MouseEvent):void{
stage.removeEventListener(TimerEvent.TIMER, keko);
clearInterval(timerInterval);
stage.frameRate = 0.01;

}
Jeffin
  • 1,099
  • 12
  • 23