我的项目需要工作暂停和恢复代码

I need a working pause and resume code on my project

到目前为止,我尝试了很多代码暂停和玩游戏,但其中很多都没有用。

我在舞台上有一个移动的物体还有我有定时器动态文本。

我需要暂停工作并在我的项目上播放代码。

例如我的移动对象

/*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;

我的定时器代码

更新:@www0z0k 你的意思是 this.The 定时器暂停和鼠标悬停错误不存在 anymore.But 当我点击播放按钮时定时器没有恢复。

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);
} 

最后一个代码我 tried.But 它不能正常工作。已更新

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;

}

不是特定于您的代码,但您会明白的

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);
    }
}

我认为可能有一个小错误,您已将事件侦听器添加到 this 并尝试从 stage 中删除事件侦听器。试试这个代码,如果这不起作用让我知道我会为你提供另一个解决方案

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;

}