开发者

Not understanding a "term is undefined" error

Here is an example of the code I'm working on:

package button
{
    import flash.display.*;
    import flash.events.*;

    public class  Button extends MovieClip
    {
         public var mybutton:MovieClip;

         public function Button () {
             buttonMode=true;
             mybutton.fromFile.addEventListener (MouseEvent.CLICK , handle )
         }

         public function开发者_StackOverflow社区 handle (e:MouseEvent) {
             trace ("hello")
         }
    }
}

Click here to download an example

It gives me the following error, and I can't figure out why:

TypeError: Error #1010: A term is undefined and has no properties.
    at button::Button()
    at test()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()

Note that I do have auto declare enabled in my script settings. How should I fix this?


First of all, you define mybutton as MovieClip but never actually create it using the "new" keyword, nor do you assign that reference to any existing object, so that reference is "null" by default. But even if you did create a new MovieClip properly like so:

     public var mybutton:MovieClip;

     public function Button () {
         mybutton = new MovieClip();

         if(stage){
             stage.addChild(mybutton);
         }

         buttonMode=true;
         mybutton.fromFile.addEventListener (MouseEvent.CLICK , handle )
     }

You would then run into the same issue, and the offending line of code would be:

mybutton.fromFile.addEventListener (MouseEvent.CLICK , handle );

The MovieClip class is a dynamic class, which means that you can dynamically define properties of that object at runtime but until you do so, that reference is null or undefined. Take for example your code:

mybutton.fromFile

The property "fromFile" must either be the symbol name of a MovieClip that you've already created inside of myButton, or it must be defined first within your code somewhere before you can access it. You're accessing this undefined reference when you use the following code:

mybutton.fromFile.addEventListener

You're telling flash "okay, there is an EXISTING object attached to myButton called fromFile, so go get it, and attach a listener to it." Since you have never actually created an object and attached to the dynamic, user made property "fromFile", this "term" is undefined. Below is the only way that this code would work:

package button
{
    import flash.display.*;
    import flash.events.*;

    public class  Button extends MovieClip
    {
         public var mybutton:MovieClip;

         public function Button () {
             buttonMode=true;
             mybutton.fromFile = new MovieClip();
             //OR
             //mybutton.fromFile = someOtherMovieClip;
             mybutton.fromFile.addEventListener (MouseEvent.CLICK , handle )
         }

         public function handle (e:MouseEvent) {
             trace ("hello")
         }
    }
}

Note also that in order to attach a listener to any object, that Object must have either implement IEventDispatcher at some level in the objects inheritance chain or at some level the object must be derived from or extend EventDispatcher. MovieClips, Sprites and other DisplayObjects do this.

Also note that if you were to simply attach a CLICK event listener to a simple, blank new MovieClip object, you're never going to fire that event because you can never click on it. You cannot click on it, because you have not placed anything that is physically clickable inside that movieclip. The following code should work as you intended:

package button
{
    import flash.display.*;
    import flash.events.*;

    public class  Button extends MovieClip
    {
         public var mybutton:MovieClip;

         public function Button () {
             mybutton = new MovieClip();
             mybutton.graphics.beginFill(0, 1);
             mybutton.graphics.drawRect(0,0,200,200);
             mybutton.graphics.endFill();
             if(stage){
                 stage.addChild(mybutton);
             }

             mybutton.buttonMode = true;
             mybutton.fromFile.addEventListener (MouseEvent.CLICK , handle )
         }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜