Flash AS3从MovieClip访问全局变量

时间:2011-03-21 00:23:53

标签: flash actionscript-3 global-variables movieclip flash-cs5

我有一个爆炸的影片剪辑,这是在代码中完成的,因为我随机化了爆炸中碎片的方向和数量,因此它是一个带有一帧的影片剪辑,所有动画都是用代码完成的。问题是,当玩家按下“p”时,我试图从主时间线暂停游戏。现在我拥有它所以它将变量gamePaused = true并调用函数pauseGame()来停止其他一切。但是我不知道如何从爆炸影片剪辑的代码中访问变量gamePaused。如果我能以某种方式检查影片剪辑中的变量,我可以暂停该动画,直到玩家再次按下“p”。

基本上,如何从影片剪辑中访问主时间轴中的变量?

同样要指出的是,所有这些爆炸都是在主时间轴的代码中创建为精灵,我在网上找到的任何解决方案都不喜欢这样。所以请记住这一点。

这是主时间轴代码:

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

1 个答案:

答案 0 :(得分:1)

您可以将根目录转换为movieClip以获取它的动态属性:

MovieClip(root).gamePaused

这种写入总是很繁琐,因此您可以存储对它的引用:

var top:MovieClip = MovieClip(root);

top.gamePaused

希望有帮助...