A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS3 - KeyboardEvent in class

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3

    AS3 - KeyboardEvent in class

    Hi guys! I'm am new with the AS3 and this is my first thread ever! So grats to me!

    I ran into a small problem today when i was coding and was hoping someone could explain to me how to revolve that. So here it goes.

    I have three files (no sub-folders) in my folder: test.fla test.as and car.as
    All i wanna do is to be able to move my movieclip with arrow keys. But i want that to be coded in class car.as. Cuz for some reason it works perfectly when i write the code in my test.as

    here is the code i am using for when it dosen't work:

    test.as
    Actionscript Code:
    package  
    {
        import flash.display.MovieClip;

        public class test extends MovieClip
        {
            public function test()
            {
                // constructor code
                var car:Car = new Car();
                addChild(car);
            }
        }
    }

    car.as
    Actionscript Code:
    package  
    {  
        import flash.display.MovieClip;
        import flash.events.KeyboardEvent;
       
        public class Car extends MovieClip
        {
            public function Car()
            {
                // constructor code
                x = 200;
                y = 300;
                addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            }
           
            public function keyPressed(event:KeyboardEvent)
            {
                if (event.keyCode == 65)
                {
                    x -= 5;
                }
            }
        }
    }

    Btw when i use MouseEvent.CLICK in car.as for moving the object it works perfectly. So i guess it has something to do with stage not recognizing keypresses or something like that.

    Well anyways, I hope you understand my problem. Looking forward for some help! Best of luck!

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    You need a reference to the stage from the Car class.

  3. #3
    Senior Member
    Join Date
    Jan 2011
    Posts
    171

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center