Flash AS3 Access Global Variables From A MovieClip
I have a movie clip of an explosion which is done in code because I am randomizing the direction and amount of debris from the explosion, so it's a movie clip with one frame, and all animation is done in code. Problem is, I'm trying to pause the game from the main timeline when the player presses "p". Right now I have it so it turns the variable gamePaused = true and calls the function pauseGame() which stops everything else. However I don't know how to access the variable gamePaused from inside the explosion movie clip's code. If I can somehow check that variable in the movie clip, I can pause that animation until the player presses "p" again.
So basically, how do I access a variable in the main timeline from a movie clip?
Also just to point out, all of these explosions were created as Sprites in the main timeline's code, any solutions I have found online didn't like that. So just keep that in mind.
Here's 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();
}
}
You can cast the root to a movieClip to get at all it's dynamic properties:
MovieClip(root).gamePaused
This can be kind of tedious to write all the time so you can store a reference to it:
var top:MovieClip = MovieClip(root);
top.gamePaused
Hope that helps...
精彩评论