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

「init」メソッドまたはクラス コンストラクター内に eventListeners を配置する必要があるため、これは論理的です。

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 に答える