A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: KeyboardEvent in class

Threaded View

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

    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 (Code works when I put everything intest.as). I guess i have to link it somehow to the stage, but can't find out why and what.

    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 instead 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!
    Last edited by thejmbstyle; 04-19-2012 at 07:46 AM.

Tags for this Thread

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