Flash AS3 从影片剪辑访问全局变量
Posted
技术标签:
【中文标题】Flash AS3 从影片剪辑访问全局变量【英文标题】:Flash AS3 Access Global Variables From A MovieClip 【发布时间】:2011-07-19 08:40:00 【问题描述】:我有一个爆炸的影片剪辑,它是用代码完成的,因为我正在随机化爆炸产生的碎片的方向和数量,所以它是一个只有一帧的影片剪辑,所有的动画都是用代码完成的。问题是,当玩家按下“p”时,我试图从主时间线暂停游戏。现在我有了它,所以它把变量 gamePaused = true 并调用函数 pauseGame() 来停止其他一切。但是我不知道如何从爆炸影片剪辑的代码中访问变量 gamePaused。如果我能以某种方式检查影片剪辑中的变量,我可以暂停该动画,直到播放器再次按下“p”。
所以基本上,我如何从影片剪辑访问主时间轴中的变量?
还要指出,所有这些爆炸都是在主时间线的代码中作为 Sprite 创建的,我在网上找到的任何解决方案都不喜欢这样。所以请记住这一点。
这是主要的时间线代码:
//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】:您可以将根转换为movieClip 以获得它的所有动态属性:
MovieClip(root).gamePaused
这可能有点乏味,所以你可以存储对它的引用:
var top:MovieClip = MovieClip(root);
top.gamePaused
希望对您有所帮助...
【讨论】:
成功了,一直抛出一些未定义的对象错误,但是在我得到变量的地方尝试/捕获清除了它。非常感谢。以上是关于Flash AS3 从影片剪辑访问全局变量的主要内容,如果未能解决你的问题,请参考以下文章