user1426572
user1426572

Reputation:

Several errors "Access of undefined property"

So I have this pretty basic code in my document class:

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

The problem is that I keep getting errors that to a beginner don't make any sense:

"Call to a possibly undefined method addEventListener." x 3 "Access of undefined property onEnter." "Access of undefined property onKeyboardUp." "Access of undefined property onKeyboardDown."

I really don't understand this issue. How can AS3 not recognize addEventListener? As well, I did have it so my event listeners were added to the stage "stage.addEventListener" and it wasn't recognizing the stage either. Can somebody push me in the right direction with this issue? Thanks!

Upvotes: 0

Views: 3031

Answers (2)

The_asMan
The_asMan

Reputation: 6402

All in all your code is almost there you just need a little bit better understanding on how the display list works.

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

Upvotes: 0

CGBe
CGBe

Reputation: 183

It's logic because you'll have to place the eventListeners inside the ´init´ method or Class constructor.

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

If not the listeners are placed outside the class scope, and therefor not recognized.

Good luck!

Upvotes: 1

Related Questions