Actionscript 3 “几个错误”;访问未定义的财产”;

Actionscript 3 “几个错误”;访问未定义的财产”;,actionscript-3,addeventlistener,stage,Actionscript 3,Addeventlistener,Stage,所以我的文档类中有一个非常基本的代码: package { import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.ui.*; import flash.events.MouseEvent; import flash.display.Stage; import flash.display.M

所以我的文档类中有一个非常基本的代码:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.*;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.display.MovieClip;

    public class Main extends Sprite
    {
        //Properties
        public var circle:Circle;
        public var vx:Number;
        public var vy:Number;   
        addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
        addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
        addEventListener(Event.ENTER_FRAME, onEnter);
        public function addedToStageHandler(event:Event):void
        {

        }
        public function Main()
        {
            super();
            init();
        }
        public function init():void
        {
            vx = 0;
            vy = 0;

            circle = new Circle(35, 0x0066FF);
            stage.addChild(circle);
            circle.x = 50;
            circle.y = 50;          


        }
        public function onKeyboardDown(event:KeyboardEvent):void
        {
            switch(event.keyCode)
            {
                case Keyboard.LEFT:
                vx = -5;
                break;
                case Keyboard.RIGHT:
                vx = 5;
                break;
                case Keyboard.UP:
                vy = -5;
                break;
                case Keyboard.DOWN:
                vy = 5;
                break;
            }
        }
        public function onKeyboardUp(event:KeyboardEvent):void
        {
            switch(event.keyCode)
            {
                case Keyboard.LEFT:
                vx = 0;
                break;
                case Keyboard.RIGHT:
                vx = 0;
                break;
                case Keyboard.UP:
                vy = 0;
                break;
                case Keyboard.DOWN:
                vy = 0;
                break;
            }
        }
        public function onEnter(event:Event):void
        {
            circle.x += vx;
            circle.y += vy;
        }
    }
}
问题是我不断地犯一些对初学者来说毫无意义的错误:

“对可能未定义的方法addEventListener的调用。”x 3 “访问未定义的属性OneNoter。” “访问键盘上未定义的属性。” “在KeyboardDown上访问未定义的属性。”


我真的不明白这个问题。AS3如何不能识别addEventListener?同样,我也有它,所以我的事件监听器被添加到舞台“stage.addEventListener”中,而它也不识别舞台。有人能在这个问题上把我推向正确的方向吗?谢谢

这是一种逻辑,因为您必须将eventListeners放在“init”方法或类构造函数中

public function init():void
{
    addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
    addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
    addEventListener(Event.ENTER_FRAME, onEnter);

    vx = 0;
    vy = 0;

    circle = new Circle(35, 0x0066FF);
    stage.addChild(circle);
    circle.x = 50;
    circle.y = 50;         
} 
如果不是,则侦听器被放置在类范围之外,因此无法识别


祝你好运

总而言之,您的代码就快到了,您只需要更好地了解一下显示列表的工作原理

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.*;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.display.MovieClip;

    public class Main extends Sprite
    {
        //Properties
        public var circle:Circle;
        public var vx:Number;
        public var vy:Number;

       // we can not do function calls like this in the class declaration area
       // so we move these listeners to a function
       // addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
       // addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
       // addEventListener(Event.ENTER_FRAME, onEnter);

        public function Main()
        {
            super();
            this.init();
        }
        public function init():void
        {
            // the "this" keyword means we are scoping it to this class instance 
            this.addEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)

            // using "this" is good practice and will help make your code more readable
            this.vx = 0;
            this.vy = 0;

            this.circle = new Circle(35, 0x0066FF);
            stage.addChild(circle);
            this.circle.x = 50;
            this.circle.y = 50;          


        }
        public function addedToStageHandler(event:Event):void
        {
            // doing addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
            // will set the scope for this listener to this class
            // you want to target the stage. And since we are waiting for ADDEDTOSTAGE
            // to trigger we know we are on the stage.
            // the only time we can access stage is if we are on the display list.

            // clean up the listener since we do not need it anymore
            this.removeEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)

            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
            stage.addEventListener(Event.ENTER_FRAME, onEnter);

        }
        public function onKeyboardDown(event:KeyboardEvent):void
        {
            switch(event.keyCode)
            {
                case Keyboard.LEFT:
                  this.vx = -5;
                  break;
                case Keyboard.RIGHT:
                  this.vx = 5;
                  break;
                case Keyboard.UP:
                  this.vy = -5;
                  break;
                case Keyboard.DOWN:
                  this.vy = 5;
                  break;
            }
        }
        public function onKeyboardUp(event:KeyboardEvent):void
        {
            switch(event.keyCode)
            {
                case Keyboard.LEFT:
                  this.vx = 0;
                  break;
                case Keyboard.RIGHT:
                  this.vx = 0;
                  break;
                case Keyboard.UP:
                  this.vy = 0;
                  break;
                case Keyboard.DOWN:
                  this.vy = 0;
                  break;
            }
        }
        public function onEnter(event:Event):void
        {
            this.circle.x += this.vx;
            this.circle.y += this.vy;
        }
    }
}

当我这么做的时候,我犯了更多的错误!谢谢你的尝试:)。什么错误?上面的代码很好,只要你的问题中有名为onkeyboardown、onKeyboardUp和onEnter的函数。我在代码中有这个函数,和前面的代码一样,我得到了20个错误,都是“访问未定义属性vx”的实例你的事件监听器需要在类被添加到舞台之后添加。除非将类添加到显示列表中,否则您无权访问阶段事件。在这种情况下,应将Main类设置为document类。你需要一些类似的东西。addEventListener(EVENT.AddedStatage,AddedStatage)。好的,我有点明白了。那么,我应该为参数“AddedStatage”编写什么代码呢?谢谢!您的代码只有一个问题(除了AddedStatage需要更改为ADDED_to_STAGE;);当我运行您的编辑时,我得到一个错误,说它在第26行的super之前需要一个标识符。知道该怎么做吗?嗨,所以我从代码中删除了“this.super();”,现在它可以正常工作了;super到底做什么?不使用它被认为是不好的做法吗?使用它而不出现编译器错误的任何解决方法?再次感谢。是的,它应该是super(),而不是这个。super()super所做的是调用基类的构造函数,即:您要扩展的类。最好总是调用super,以防基类构造函数中存在设置类的代码。