使用ActionScript 2暂停游戏

时间:2014-10-20 23:02:10

标签: actionscript-2 flash-cs6 pausing-execution

我制作的游戏中玩家必须避免随意掉落的物体。我不知道如何实现暂停。我已经被困在这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);
            }
        }
};

我决定使用密钥,因为我在尝试使用按钮时找不到任何解决方案。暂停功能无法正常工作,我需要输入密钥&#39; p&#39;几次暂停它,但我不希望框架重置并在我恢复时加载更多随机对象。

2 个答案:

答案 0 :(得分:1)

试试这个:

删除你的:

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

答案 1 :(得分:0)

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

stage.frameRate = 0.01;