2015-11-21 42 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 ...

希望能有所幫助。

相關問題