简体   繁体   English

AS3:无法创建 Movieclip 类实例?

[英]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.我只是在学习如何使用一些非常基本的代码在 AS3 中使用类,我正在努力弄清楚如何做最简单的事情。

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.我已将文档类设为“Test”类(本质上是我的主类),我试图用它做的就是将“WhiteBall”类(影片剪辑)的一个实例添加到舞台上。

The 'WhiteBall' class is supposed to allow me to control the movieclip with the keyboard. 'WhiteBall' 类应该允许我用键盘控制影片剪辑。 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.类型错误:错误 #1009:无法访问空对象引用的属性或方法。 at WhiteBall$iinit()[/Users/Owner/Desktop/Animation/Coding/WhiteBall.as:13] at Test$iinit()[/Users/Owner/Desktop/Animation/Coding/Test.as:11]在 WhiteBall$iinit()[/Users/Owner/Desktop/Animation/Coding/WhiteBall.as:13] 在 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:这是“WhiteBall”类的代码:

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: 'Test' 类中的第 11 行错误是指这一行:

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. Organis 所说的是这样的:当您创建一个 DisplayObject,例如一个 MovieClip,它是您的 WhiteBall 扩展的类,那么该对象的 stage 属性为空。 That means that when you tried to access the stage property of your whiteball on its constructor这意味着当您尝试在其构造函数上访问白球的 stage 属性时

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

an error was thrown because stage was null.因为 stage 为空,所以抛出了一个错误。 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.因此,当您的 Test 类实例将球添加到舞台时,您的 WhiteBall 实例的 stage 属性将具有一个值,因此可以安全地侦听任何键盘事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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