简体   繁体   中英

AS3: Can't create Movieclip class instance?

I'm just learning how to use classes in AS3 using some very basic code and I'm tearing my hair out trying to figure out how to do the simplest thing.

I've made the document class the 'Test' class (essentially my main class), and all I'm trying to do with it is add an instance of the 'WhiteBall' class (a movieclip) to the stage.

The 'WhiteBall' class is supposed to allow me to control the movieclip with the keyboard. I don't know if this part works yet, because I keep getting this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at WhiteBall$iinit()[/Users/Owner/Desktop/Animation/Coding/WhiteBall.as:13] at Test$iinit()[/Users/Owner/Desktop/Animation/Coding/Test.as:11]

Here is the code for the 'Test' class:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;



    public class Test extends MovieClip
    {
        var whiteBall:WhiteBall = new WhiteBall ();

        public function Test() {

            addEventListener(Event.ENTER_FRAME, whiteBallSpawn);

        }

        public function whiteBallSpawn(evt:Event) {


            stage.addChild(whiteBall);
            whiteBall.x = 200;
            whiteBall.y = 250;




        }





    }
}

Here is the code for the 'WhiteBall' class:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class WhiteBall extends MovieClip
    {


        public function WhiteBall() {

            stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
        }

        public function keysdown(mykey:KeyboardEvent) {

        if(mykey.keyCode==Keyboard.UP) {
            this.y--;
        }

        if(mykey.keyCode==Keyboard.DOWN) {
            this.y++;
        }

        if(mykey.keyCode==Keyboard.RIGHT) {
            this.x++;
        }

        if(mykey.keyCode==Keyboard.LEFT) {
            this.x--;
        }
    }
    }
}

The line-11 error in the 'Test' class refers to this line:

var whiteBall:WhiteBall = new WhiteBall ();

I have no idea what the problem is here. Any help you could give me would be really appreciated.

What Organis said is this: When you create a DisplayObject, for example a MovieClip which is the class that your WhiteBall extends, then the stage property of that object is null. That means that when you tried to access the stage property of your whiteball on its constructor

public function WhiteBall() 
{
   stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
} 

an error was thrown because stage was null. So to solve this the safest and most common way is to wait until the ball is added to stage and then listen for any stage keyboard events. Like so:

public function WhiteBall() 
{
    if(stage != null)
    {
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
    }
    else
    {
       this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }
}
private function addedToStage(e:Event):void
{
   this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
   stage.addEventListener(KeyboardEvent.KEY_DOWN, keysdown);
}

So the moment your Test class instance will add the ball to the stage, then the stage property of your WhiteBall instance will have a value and therefore it will be safe to listen for any keyboard events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM