0

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

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“访问未定义的属性 onEnter。” “访问未定义的属性 onKeyboardUp。” “访问未定义的属性 onKeyboardDown。”

我真的不明白这个问题。AS3 怎么不能识别 addEventListener?同样,我确实拥有它,所以我的事件侦听器被添加到舞台“stage.addEventListener”中,它也无法识别舞台。有人可以在这个问题上将我推向正确的方向吗?谢谢!

4

2 回答 2

1

这是合乎逻辑的,因为您必须将 eventListeners 放在“init”方法或 Class 构造函数中。

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;         
} 

如果不是,则侦听器被放置在类范围之外,因此无法识别。

祝你好运!

于 2012-05-30T17:19:12.717 回答
0

总而言之,您的代码几乎就在那里,您只需要更好地了解显示列表的工作原理。

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;
        }
    }
}
于 2012-05-31T01:28:24.570 回答