当前位置: 动力学知识库 > 问答 > 编程问答 >

flash - AS3 Error 1120, after extending a class

问题描述:

I don't how to describe the situation in short, so I am going to describe it in details.

I have created a class, extended from MovieClip. It looks like this:

 // Libraries are imported

public class WindowObject extends MovieClip {

public function WindowObject():void {

dragArea_mc.addEventListener(...);

}

public function AFunction():void {

if(close_mc == null) {

// Do something

}

}

}

Where dragArea_mc is (or should be) defined in the MovieClips that use this as the base class.

I have created a MovieClip, with two children MovieClips named as dragArea_mc and close_mc, and set its base class to be the one on top. It worked fine. No problem at all.

Since different windows have different behaviors, I wanted to create some other classes based on WindowObject. (i.e. they extends WindowObject, and have additional functions.)

I changed the base class of the MovieClip created in step 2 to be the new class. Now, here is the problem:

 1120: Access of undefined property dragArea_mc.

1120: Access of undefined property close_mc.

When I clicked on the errors, they redirected me to the WindowObject class.

If I don't create new classes, but just write the additional functions on the MovieClip directly, and set back the base class to be WindowObject, it works fine again.

网友答案:

Okay, I just fired up flash cs5, and reproduced your issue. And I have to admit I am a bit puzzled... And I'm going to have to investigate a little more, because I'm not sure I really understand what is going on.

Try setting both classes to public dynamic class...

and then instead of referencing dragArea_mc directly, reference it through this.dragArea_mc

what I ended up having:

public dynamic class TestParent extends MovieClip {
    public function TestParent() {
        // trace(drag_mc); doesn't work
        trace(this.drag_mc); // works as long as the class is dynamic
    }
}

public dynamic class TestChild extends TestParent {
    public function TestChild() {
        // trace(drag_mc); surprisingly does work, but odd it doesn't work in the parent.
        trace(this.drag_mc); // works as long as the class (and its parent) are dynamic
    }
}

And then in the UI, created a new MovieClip and set its base class to TestChild and created a child movieclip with an instance name of drag_me.

... I'm going to investigate more, and edit this post later, but try that and see if it helps fix your problem.

网友答案:

it may be undefined because it isn't on the stage quite yet..try it like this

// Libraries are imported

class WindowObject extends MovieClip {
    public function WindowObject():void {
        addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        dragArea_mc.addEventListener(...);
    }

    public function AFunction():void {
        if(close_mc == null) {
            // Do something
        }
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: