Flash AS3 получает доступ к глобальным переменным из мувиклипа - PullRequest
3 голосов
/ 21 марта 2011

У меня есть видеоклип взрыва, который сделан в коде, потому что я рандомизирую направление и количество мусора от взрыва, так что это видеоклип с одним кадром, и вся анимация выполняется в коде. Проблема в том, что я пытаюсь приостановить игру с основной временной шкалы, когда игрок нажимает «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 Ответ

1 голос
/ 21 марта 2011

Вы можете преобразовать корень в мувиклип, чтобы получить все его динамические свойства:

MovieClip(root).gamePaused

Это может быть утомительно писать постоянно, поэтому вы можете сохранить ссылку на него:

var top:MovieClip = MovieClip(root);

top.gamePaused

Надеюсь, это поможет ...

...