2016-11-17 43 views
1

基本上我編碼通過點擊按鈕來切換場景。我將框架標籤砂場景名稱作爲參數。 MovieClip(根).gotoAndStop(frameLabel,sceneName);在舞臺上正常工作。但是,當我在類上使用相同的引發警告TypeError:錯誤#1009:無法訪問空對象引用的屬性或方法。我知道這是因爲班級沒有根。有什麼方法可以解決這個問題。請找到下面的代碼。參考舞臺動作腳本3

//類代碼

package { 
    import flash.events.KeyboardEvent; 
    import flash.events.MouseEvent; 
    import flash.display.SimpleButton; 
    import flash.display.*; 
    import flash.text.*; 
    import flash.events.Event; 
    import flash.display.MovieClip; 

    public class ClickButton extends SimpleButton { 
    public var fLabel:String; 
    public var sName:String; 
    public var sNumber:Number; 

    public function ClickButton() 
    { 

    }  

    public function GotoSession(sesBut:SimpleButton, frameLabel:String, sceneName:String):void {   
    sesBut.addEventListener(MouseEvent.CLICK, gotoSes);   
    function gotoSes(event:MouseEvent):void {  
    MovieClip(root).gotoAndStop(frameLabel, sceneName); 
    } 
    } 
} 

// AS3代碼

var btn1 = new ClickButton(); 
addChild(btn1); 
btn1.GotoSession(home, "menu", "Home"); 

回答

1

你有兩個問題在這裏,我真的不明白,爲什麼你這樣的工作,但是,

1:當我嘗試編譯你的代碼時,我得到一個編譯時間錯誤:

在Y末尾錯過了一個「}」我們班。

我刪除了什麼不習慣:

package { 
    import flash.events.MouseEvent; 
    import flash.display.SimpleButton; 
    import flash.display.MovieClip; 
    public class ClickButton extends SimpleButton { 
     public function ClickButton() { 
     } 
     public function GotoSession(sesBut:SimpleButton,frameLabel:String,sceneName:String):void { 
      sesBut.addEventListener(MouseEvent.CLICK,gotoSes); 
      function gotoSes(event:MouseEvent):void { 
       MovieClip(root).gotoAndStop(frameLabel,sceneName); 
       // and if You want to remove the ClickButton instance :; 
       // ADD those two lines : 
       sesBut.removeEventListener(MouseEvent.CLICK,gotoSes); 
       MovieClip(root).removeChild(sesBut); 
       // DO NOT forget to remove the Listeners before to remove an instance! 
      } 
     } 
    } 
} 

我想,你有你的庫按鈕鏈接到ClickButton類,如下所示:

enter image description here

所以:

var btn1:ClickButton = new ClickButton(); 
addChild(btn1); 
btn1.GotoSession(btn1, "menu", "Home"); 
stop(); 

如果我點擊btn1,這會讓我感覺t他在「菜單」標籤處顯示「Home」。

這工作就像一個魅力。

的的FrameLabel「菜單」

stop(); 
trace("currentScene.name = " + this.currentScene.name); 
trace("currentFrameLabel = " + this.currentFrameLabel); 

/* 
OUTPUT : 
currentScene.name = Home 
currentFrameLabel = menu 
*/ 

[編輯]

如果我設置能見度爲false,然後再以真實的,我也有同樣的問題,如果我想改變阿爾法財產mc_1。 這適用於我的文件:

import flash.display.MovieClip; 
stop(); 
trace("currentScene.name = " + this.currentScene.name); 
trace("currentFrameLabel = " + this.currentFrameLabel); 
var mc_1:MovieClip = mc_1; 
// If I don't add this line, I have the same problem when I set the visibility to true 
var mc_2:MovieClip = mask_mc; 
var mc_3:MovieClip = red_mc; 
// I do the same for mc_2 labeled "mask_mc" 
// mc_1 is now always recognized as a MovieClip as mc2. 
mc_1.visible = false; 
mc_1.visible = true; 
// No more problem if I add the line var mc_1:MovieClip = mc_1; 
// If I don't do this, I cannot access mc_1 as a MovieClip 
mc_1.alpha = 0.5; 
mc_1.mask = mc_2; 
mc_3.alpha = 0.5; 
mc_3.visible = false; 
mc_3.visible = true; 
mc_3.alpha = 0.9; 
// It seems that You have to declare the MC variables before to change the properties 

[/編輯]

,但我不明白你行:

//btn1.GotoSession(home, "menu", "Home"); 

家爲空(您沒有任何參考一個名爲home的ClickButton)......

+0

:)。非常感謝。有用。 – Rajesh

+0

爲你而歡快@Rajesh 我從來沒有在AS3創建過一個場景,所以我現在甚至都沒有創建一個,這對我來說是最困難的。 但我認爲你可以改善你的代碼... – tatactic

+0

所以這就是你想問在http://stackoverflow.com/questions/40603368/accessing-root-from-the-class-in-action-script- 3?現在更清楚了!爲你的問題+1。 – tatactic

1

你也可以創建一個ClickSomeButton類 用靜態方法 「gotoLand」

package { 
import flash.events.MouseEvent; 
import flash.display.SimpleButton; 
import flash.display.MovieClip; 

public class ClickSomeButton{ 
    public static function gotoLand(target:MovieClip,sesBut:SimpleButton,frameLabel:String,sceneName:String):void { 
      sesBut.addEventListener(MouseEvent.CLICK,gotoSes); 
      function gotoSes(event:MouseEvent):void { 
       target.gotoAndStop(frameLabel,sceneName); 
       target.removeEventListener(MouseEvent.CLICK,gotoSes); 
       target.removeChild(sesBut); 
      } 
     } 
    } 
} 

然後在FLA:

var btn2:Btn2 = new Btn2(); 
addChild(btn2); 
btn2.x = 200; 
ClickSomeButton.gotoLand(this,btn2, "menu", "Home") 
stop(); 

符號屬性BTN2庫:

library symbol properties

這只是因爲我不知道爲什麼你有,如果你把這個實例的方法...... 就像你在原來的問題沒有通過按鈕作爲一個參數:

//btn1.GotoSession(btn1, "menu", "Home"); 

這是奇怪的您的ClickButton類...

+0

@Rajesh這是不那麼棘手的AMO。 – tatactic