开发者

Resolving a 1069 error between a child object and a parent function

I've shortened the code samples below so that it's more readable. Here's the rub:I create a whole bunch of MovieClips containing the letters a-z. These MovieClips are children of a parent MovieClip called "levelTwo", where levelTwo is a kind of a logic manager for the level.

When they're clicked, they send off some data to levelTwo for evaluation (It's Hangman). Problem is that on click I get a 1069 error. It WAS a 1061 error until I added the event.target.parent bit in LetterButton.

Here's the relevant code:

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);

    }

}

} And the relevant bit from 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++)
            {
   开发者_Python百科             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(); 
            }
    }


I see no magic here. You subscribe to LetterButton instance, so you get event.target reference pointing to it.

I think a better approach would be to subscribe to parent clip, that contains all the LetterButton instances. MouseEvent.CLICK is a bubbling event, so you will get your handler triggered on parent every time something is clicked inside it.

See the sample code.

// 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜