Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:14:39+00:00 2026-06-04T21:14:39+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:14:40+00:00Added an answer on June 4, 2026 at 9:14 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some pretty basic jQuery code: ... $(this).find('img').load(function(){ loadedImages++; if(loadedImages == $this.find('img').length){ ...
This is a pretty basic C++ design question: I have a class that contains
I realize this question is pretty basic, but I'm really stuck. I have a
I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
This is pretty much the same problem i have, except with very different code:
Okay, so, this code is pretty basic. The user inputs an answer into a
I have some pretty basic C++ code: #include <iostream> #include <string.h> #include <msclr\/marshal_cppstd.h> #using
This is pretty basic question around unit testing. I have a method e.g. GetOrderDetails
So this seems pretty basic but I can't get it to work. I have
Alright so pretty basic, here are the pieces of code I have, basically it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.