2015-11-21 38 views
0

嘿,我想使用一个变量两个不同的影片剪辑。从另一个影片剪辑访问变量

我有一个影片剪辑(mainGame)使用此代码

onClipEvent(load) 
{ 
    var light:Boolean; 
    light = true; 
    trace("Game Loaded"); 

} 

on(keyPress "x") 
{ 
    if(light == false) 
    { 
     light = true; 
     trace("light is on"); 
    } 
    else if(light == true) 
    { 
     light = false; 
     trace("light is off"); 
    } 
} 

此代码切换一个布尔值。

现在我有另一个MovieClip(敌人),我想在其中访问布尔“光”,然后根据布尔值使此MovieClip(敌人)可见或不可见。

onClipEvent(enterFrame) 
{ 
    if(light == true) 
    { 
     this._visible = false; 
    } 
    else if(light == false); 
    { 
     this._visible = true; 
    } 
} 

感谢你的帮助, 若昂·席尔瓦

回答

0

要从enemy一个访问mainGame影片剪辑的light变量,你可以这样做:

_root.mainGame.light 

所以,你的代码可以像这样,例如:

// mainGame 

onClipEvent(load) 
{ 
    var light:Boolean = true; 
    trace('Game Loaded'); 
} 

on(keyPress 'x') 
{ 
    // here we use the NOT (!) operator to inverse the value 
    light = ! light; 

    // here we use the conditional operator (?:) 
    trace('light is ' + (light ? 'on' : 'off')); 
} 

// enemy 

onClipEvent(enterFrame) 
{ 
    this._visible = ! _root.mainGame.light; 
} 

,你也可以从mainGame影片剪辑做:

// mainGame 

on(keyPress 'x') 
{ 
    light = ! light; 

    _root.enemy._visible = ! light; 

    trace('light is ' + (light ? 'on' : 'off')); 
} 

欲了解更多有关NOT (!) operator,看看here,以及有关conditional operator (?:),从here

正如你学习,你就可以开始learning ActionScript3here ...

希望能有所帮助。

相关问题