0

我正在制作一个玩家必须避免随机坠落物体的游戏。我不知道如何实现暂停。我已经坚持了2天了!

我尝试使用 gotoAndPlay 等,但对象继续在后台运行。当我重新开始游戏时,它们仍在下落,看起来框架重置并加载了新的随机下落物体。

var steps:Number = 10;
var spriteX:Number = 280;
var spriteY:Number = 650;
var alienCounter=1;
var asteroidCounter=1;
var live:Number=3;
var depthLevel=3000;
var score:Number = 0;
var gamePaused;
dropTimer=setInterval(createAlien,2000);
drpTimer2=setInterval(createAsteroid,1000);
//---- functions ----
function checkKeys() 
{
if (Key.isDown(Key.RIGHT)) 
{
spriteX += steps;
}
else if (Key.isDown(Key.LEFT)) 
{
spriteX -= steps;
}
}
function updateSpaceship() 
{
ship._x = spriteX;
ship._y = spriteY;
}
function createAlien()
{
    var curr_alien;
    curr_alien=attachMovie("alien","alien"+alienCounter,depthLevel);
    curr_alien._y=40;
    curr_alien._x=Math.random()*510+20;
    curr_alien._xscale=curr_alien._yscale=50;
    curr_alien.speed=Math.random()*10+3;
    alienCounter+=1;
    depthLevel+=1;
    curr_alien.onEnterFrame=alienMove;
}
function alienMove()
{
    if(!gamePaused)
    {
    this._y+=this.speed;
    if(this.hitTest(ship))
    {
        score += 1;
        trace(score);
        removeMovieClip(this);
    }
    }
}
function createAsteroid()
{
        var curr_asteroid;
        curr_asteroid=attachMovie("asteroid","asteroid"+asteroidCounter,depthLevel);
        curr_asteroid._y=40;
        curr_asteroid._x=Math.random()*510+20;
        curr_asteroid._xscale=curr_asteroid._yscale=50;
        curr_asteroid.speed=Math.random()*10+3;
        asteroidCounter+=1;
        depthLevel+=1;
        curr_asteroid.onEnterFrame=asteroidMove;
}
function asteroidMove()
{
    if(!gamePaused)
    {
    this._y+=this.speed;
    if(this.hitTest(ship))
    {
        live -= 1;
        trace(live);
        removeMovieClip(this);
        if(live<=0)
        {   
            gotoAndPlay(5);
        }
    }
    }

}
this.onEnterFrame = function() 
{
        checkKeys();
        updateSpaceship();
        if(Key.isDown(80))
        {
            if(!gamePaused)
            {
                gamePaused=true;
                gotoAndPlay(4);
            }
            else
            {
                gamePaused=false;
                gotoAndStop(3);
            }
        }
};

我决定改用钥匙,因为我在尝试使用按钮时找不到任何解决方案。暂停功能没有像我预期的那样工作,我需要多次输入键“p”来暂停它,但我不希望框架重置并在我恢复时加载更多随机对象。

4

2 回答 2

0

尝试这个:

删除您的:

this.onEnterFrame = function() 
{
        checkKeys();
        updateSpaceship();
        if(Key.getCode() == 80)
        {

            if(!gamePaused)
            {
                gamePaused=true;
                gotoAndPlay(4);
                trace("AAA")
            }
            else
            {
                gamePaused=false;
                gotoAndStop(3);
                trace("BBB")
            }
        }
};

添加这个(按键盘上的“P”键暂停):

var keyListener:Object = new Object();
keyListener.onKeyUp = function() {
    if(Key.getCode() == 80) {
        gamePaused = !gamePaused;
        mainLoop();
    }
};
Key.addListener(keyListener);

function mainLoop() {
    if (gamePaused) {
        gotoAndStop(4); // update game actions here
        trace("game is paused");
        return;
    }
    gotoAndStop(3); // update game actions here
    trace("game is running");
}

或者,如果您需要使用按钮暂停:

my_pause_Button.onRelease = function() { // your button instance name 'my_pause_Button'
     gamePaused = !gamePaused;
     mainLoop();
};
于 2014-10-21T03:31:08.473 回答
0

把它放在你的按钮功能或 keydown 功能中:

stage.frameRate = 0.01;
于 2015-04-28T03:17:50.997 回答