2011-07-01 36 views
0

我縮短了下面的代碼示例以便它更具可讀性。這裏是揉搓:我創建了一大堆包含字母a-z的影片剪輯。這些影片剪輯是名爲「levelTwo」的父級影片剪輯的子代,其中levelTwo是該級別的一種邏輯管理器。解決子對象和父函數之間的1069錯誤

當他們被點擊時,他們發送一些數據到levelTwo評估(這是Hang子手)。問題是,點擊我得到一個1069錯誤。它是一個1061錯誤,直到我在LetterButton中添加了event.target.parent位。

下面是相關代碼:

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

public class LetterButton extends MovieClip { 

    var buttonText:String; 

    public function LetterButton(lText:String,objX:int,objY:int) 
    { 
     trace ("Creating new button"); 
     x = objX; 
     y = objY; 
     buttonText = lText; 
     letterText.text = buttonText; 
     this.stop(); 
     addEventListener(MouseEvent.MOUSE_OVER,onMouseOver); 
     addEventListener(MouseEvent.MOUSE_OUT,onMouseOut); 
     this.addEventListener(MouseEvent.CLICK,onMouseClick); 
    } 

    private function onMouseOver(event:Event):void 
    { 
     gotoAndStop(2); 
     letterText.text = buttonText; 
     //trace ("You're over me and my text is " + buttonText); 
    } 

    private function onMouseOut(event:Event):void 
    { 
     gotoAndStop(1); 
     letterText.text = buttonText; 
     //trace ("You're out of me and my text is " + buttonText); 
    } 

    private function onMouseClick(event:Event):void 
    { 
     trace ("I am clicked and I am " + buttonText); 
     event.target.parent.checkGuess(buttonText); 

    } 

} 

} 而從levelTwo的相關位:

public function checkGuess(guess:String):void 
    { //Check to see if the guess matches the string 


     trace ("Guess: "+guess); 
     for(var i:int=0;i<answer.length;i++) 
      { 
       if(guess == answer.charAt(i)) 
       { 
        censoredAnswer[i] = guess; 
        trace ("Got one right"); 
        answerField.text = answerRedisplay(); //Do it now or it won't update for the check 
       } 
      } 
     if (answerField.text == answer) 
      { 
       setWin(); 
      } 
    } 
+0

管理以解決此問題:MovieClip(parent).checkGuess(buttonText); – GilloD

回答

0

我看到這裏沒有魔法。您訂閱LetterButton實例,因此您可以獲取指向它的event.target引用。

我認爲更好的方法是訂閱父剪輯,其中包含所有LetterButton實例。 MouseEvent.CLICK是一個冒泡事件,所以每次點擊內容時,您都會觸發父處理程序。

請參閱示例代碼。

// inside level two class 
addEventListener(MouseEvent.CLICK,onMouseClick); 

private function onMouseClick(event:MouseEvent):void 
{ 
    var target:LetterButton = event.target as LetterButton; 
    if (target == null) { 
     return; 
    } 
    // here we know, that some LetterButton instance was clicked 
    // and target var holds it's reference 

    // either make buttonText public, or create a getter/accessor 
    // var text:String = target.buttonText; 
    var text:String = target.getButtonTextSomehow(); 

    checkGuess(text); 
}