A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: KeyboardEvent in class

  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.

  2. #2
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    Actionscript Code:
    package {
        import flash.display.MovieClip;
        import flash.events.KeyboardEvent;

        public class DocumentClass extends MovieClip {
            private var car:Car;
            public function DocumentClass() {
                // constructor code
                car = new Car();
                addChild(car);
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            }
            public function keyPressed(event:KeyboardEvent) {
                if (event.keyCode == 65) {
                    car.x -= 5;
                }
            }
        }
    }

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

        public class Car extends MovieClip {
            public function Car() {
                // constructor code
                x = 200;
                y = 300;
            }
        }
    }

    To test this you have to Active the Disable Keyboard Shortcuts from Control tab of the Flash Player.




    marlopax
    Last edited by marlopax; 04-20-2012 at 03:12 PM.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The original problem is that to dispatch a keyboard event, the object must have keyboard focus. Unless you'd already clicked your car, and it kept focus for some reason, it won't dispatch a keyboard event (because something else has that focus). That's why normally you add keyboard listeners to the stage.
    BUT, you can't add anything to the stage during the construction of an object that has no reference to the stage. So, you add a listener for ADDED_TO_STAGE which adds the keyboard listener.
    Also, add one for REMOVED_FROM_STAGE to clean up that listener.
    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(Event.ADDED_TO_STAGE, added);
                addEventListener(Event.REMOVED_FROM_STAGE, removed);
            }
            
            public function added(event:Event):void{
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            }
    
            public function removed(event:Event):void{
                stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            }
    
            public function keyPressed(event:KeyboardEvent):void{
                if (event.keyCode == 65){
                    x -= 5; 
                }
            }
        }
    }

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Exactly what I was looking for. Very well explained too, thanks a lot!

  5. #5
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    package {
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.events.KeyboardEvent;

        public class DocClass extends MovieClip {
            private var car:Car;
            public function DocClass() {
                // constructor code
                car = new Car();
                car.addEventListener(Event.COMPLETE,completed);
                addChild(car);
                stage.addEventListener(MouseEvent.CLICK,clicked);// this is for test
            }
            private function clicked(evt:MouseEvent):void {
                removeChild(car);
            }
            private function completed(evt:Event):void {
                if (evt.target.types=="addedToStage") {
                    trace(evt.target.types);
                    evt.target.removeEventListener(Event.ADDED_TO_STAGE,evt.target.dispatchEvents);
                    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                } else if (evt.target.types=="removedFromStage") {
                    trace(evt.target.types);
                    evt.target.removeEventListener(Event.REMOVED_FROM_STAGE,evt.target.dispatchEvents);
                    stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
                }
            }
            public function keyPressed(event:KeyboardEvent):void {
                if (event.keyCode == 65) {
                    car.x -= 5;
                }
                if (event.keyCode == 83) {
                    car.x += 5;
                }
            }
        }
    }

    Actionscript Code:
    package {
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.events.EventDispatcher;

        public class Car extends MovieClip {
            public var types:String;
            public function Car() {
                // constructor code
                x = 200;
                y = 300;
                addEventListener(Event.ADDED_TO_STAGE,dispatchEvents);
                addEventListener(Event.REMOVED_FROM_STAGE,dispatchEvents);
            }
            public function dispatchEvents(evt:Event):void {
                types=evt.type;
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }
    }



    arkitx

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