actionscript-2flash-cs6pausing-execution

Pausing a game with ActionScript 2


I'm making a game where the player must avoid random falling objects. I dont know how to implement pausing. I've been stuck on this for 2 days!

I tried using gotoAndPlay and such, but the objects continue to run in the background. When I resume the game, they're still falling and it seems like the frame resets and loads new random falling objects.

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

i decided to use a key instead because i cannot find any solutions when trying to use a button. The pause function is not working as i expected, i need to enter key 'p' several times to pause it, but i dont want the frame resets and loads more random objects when i resume it.


Solution

  • try this:

    remove your:

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

    Add this (pause by pressing the 'P' key on your keyboard):

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

    Alternatively if you need to pause using a button:

    my_pause_Button.onRelease = function() { // your button instance name 'my_pause_Button'
         gamePaused = !gamePaused;
         mainLoop();
    };