Flash AS3 Access Global Variables from MovieClip

I have an explosion clip, which is done in the code, because I randomize the direction and number of debris from the explosion, so this is a one-frame clip, and the entire animation is executed in code. The problem is that I am trying to pause the game from the main timeline when the player presses "p". I have it right now, so it turns the variable gamePaused = true and calls the pauseGame () function, which stops everything else. However, I do not know how to access the gamePaused variable from the explosion fragment code. If I can somehow check this variable in the clip, I can pause this animation until the player presses p again.

So, how do I access a variable in the main timeline from a clip?

Also, to indicate that all these explosions were created as Sprites in the main timeline code, I did not like any solutions that I found on the Internet. So just keep that in mind.

Here is the main timeline code:

//This Creates An Explosion<br>
function createExplosion(explosionX, explosionY, explosionSize):void{<br>
    //This Creates The Explosion Movie Clip
    var explosionSprite:Sprite = new Sprite;
    addChild(explosionSprite);
    var explosionPic:explosionSym = new explosionSym;
    explosionSprite.addChild(explosionPic);<br>
<br>
    //This Moves It Into Position
    if (explosionSize == 3){<br>
        explosionSprite.x = explosionX + 15;<br>
        explosionSprite.y = explosionY + 15;<br>
    }<br>
    else if (explosionSize == 2){<br>
        explosionSprite.x = explosionX + 5;<br>
        explosionSprite.y = explosionY + 5;<br>
    }<br>
    else if (explosionSize == 1){<br>
        explosionSprite.x = explosionX;<br>
        explosionSprite.y = explosionY;<br>
    }<br>
    <br>
    //This Starts The Timer
    explosion[explosionsOnScreen] = explosionSprite;
    explosionTimeLeft[explosionsOnScreen] = 0;
}
//This Removes The Explosions Once Time Is Up
function explosionTimer(evt:TimerEvent):void{
    //This Declares The Variables
    var M:int = 0;

    for (M = 0; M < explosionsOnScreen; M++){
        //This Increments The Time
        explosionTimeLeft[M]++;

        //This Removes The Explosion If Enough Time Has Passed
        if (explosionTimeLeft[M] > 15){
            explosion[M].parent.removeChild(explosion[M]);
            explosion.splice(M, 1);
            explosionTimeLeft.splice(M, 1);
            explosionsOnScreen -= 1;
            break;
        }
    }
}

//This Creates Bullets To Be Used As Debris
function spawnBullets():void{<br>
    //This Declares The Variables<br>
    var M:int = 0;<br>

    //This Decides How Much Debris Will Appear
    randomDebrisNumber = (Math.round(Math.random() * 3) + 3);

    for (M = 0; M <= randomDebrisNumber; M++){
        //This Spawns The Bullets
        var debrisSprite:Sprite = new Sprite;
        addChild(debrisSprite);
        var debrisBullet:bulletSym = new bulletSym;
        debrisSprite.addChild(debrisBullet);

        //This Places The Debris
        debrisSprite.x = 0;
        debrisSprite.y = 0;

        //This Adds The Sprite To The Array
        debris[M] = debrisSprite;

        //This Gets The Direction It Moves
        do {
            debrisX[M] = ((Math.random() * 2 - 1) * 3);
            debrisY[M] = ((Math.random() * 2 - 1) * 3);
        } while ((debrisX[M] > -0.1 && debrisX[M] < 0.1) || (debrisY[M] > -0.1 && debrisY[M] < 0.1))
    }
}

//This Moves The Debris Away From The Center
function handleMoveDebris(evt:TimerEvent):void{<br>
    //This Declares The Variables<br>
    var M:int = 0;<br>

    //This Increments The Timer
    debrisTimerCount++;

    if (debrisTimerCount <= 15){
        //This Moves The Debris
        for (M = 0; M <= randomDebrisNumber; M++){
            debris[M].x += debrisX[M];
            debris[M].y += debrisY[M];
        }
    }
    else if (debrisTimerCount > 15){
        //This Removes the Debris
        for (M = randomDebrisNumber; M >= 0; M--){
            debris[M].parent.removeChild(debris[M]);
            debris.splice(M, 1);
            debrisX.splice(M, 1);
            debrisY.splice(M, 1);
        }

        //This Stops The Timer
        debrisTimerCount = 0;
        timMoveDebris.stop();
    }
}
+3
source share
1 answer

You can specify the root in movieClip to get all its dynamic properties:

MovieClip(root).gamePaused

It can be tedious to write all the time, so you can keep a link to it:

var top:MovieClip = MovieClip(root);

top.gamePaused

Hope this helps ...

+1
source

All Articles