Skip to main content
Participant
March 14, 2013
Answered

Using arrow keys (keyboard)

  • March 14, 2013
  • 3 replies
  • 1395 views

I want to make my object (chracter)  move up,down,left,right...I wrote the following code, however it seem like its NOT working any help please??

In the library Instance name is : ch1 (its a movie clip)

Stage class name is: Movment

Following is the code...what am I doing wrong ??? Any suggestion or any idea???

also, I  am getting compiler error saying: "Duplicate function defination"

package 

{

    import flash.display.MovieClip;

    import flash.events.KeyboardEvent;

    import flash.events.Event;

   

    public class Movement extends MovieClip

    {

        var ch1:Character;

        public function Movement()

        {

           

            ch1 = new Character();

        }

            // Movement

            function onKeyDown(event:KeyboardEvent):void

            {

                if (event.keyCode == keyboard.LEFT)

                {

                    vx = -5;

                }

                else if (event.keyCode == keyboard.RIGHT)

                {

                    vx = 5;

                }

                else if(event.keyCode == keyboard.UP)

                {

                    vy = -5;

                }

                else if(event.keyCode == keyboard.DOWN)

                {

                    vy = 5;

                }

            }

            function onKeyDown(event:KeyboardEvent):void

                {

                if (event.keyCode == Keyboard.LEFT ||

                    event.keyCode == keyboard.RIGHT)

                    {

                        vx = 0;

                    }

                    else if (event.keyCode == Keyboard.DOWN ||

                             event.keycode == Keyboard.UP)

                             {

                                 vy = 0;

                             }

                }

               

            //Move Character

            function onEnterFrame(event:Event): void

                {

                    // moves character

                    Character.x += vx;

                    Character.y += vy;

                }     

          

        }

                                    

    }       

}

This topic has been closed for replies.
Correct answer Ned Murphy

You have two functions named onKeyDown.  You need to have different names for all functions in the class.

3 replies

Inspiring
March 15, 2013

After further examination it shows that the code you presented is so buggy - it shouldn't even compile.

Try this:

package

{

          import flash.display.MovieClip;

          import flash.events.KeyboardEvent;

          import flash.events.Event;

          import flash.ui.Keyboard;

 

          public class Movement extends MovieClip

          {

                    private var ch1:Character;

                    private var vx:Number = 0;

                    private var vy:Number = 0;

 

                    public function Movement()

                    {

 

                              if (stage)

                                        init();

                              else

                                        addEventListener(Event.ADDED_TO_STAGE, init);

 

                    }

 

                    private function init(e:Event = null):void

                    {

                              removeEventListener(Event.ADDED_TO_STAGE, init);

                              ch1 = new Character();

                              stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

                              stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

                              addEventListener(Event.ENTER_FRAME, onEnterFrame);

                    }

 

                    private function onKeyDown(e: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;

 

                              }

                    }

 

                    private function onKeyUp(e:KeyboardEvent):void

                    {

                              switch (event.keyCode)

                              {

                                        case Keyboard.LEFT:

                                        case Keyboard.RIGHT:

                                                  vx = 0;

                                                  break;

                                        case Keyboard.UP:

                                        case Keyboard.DOWN:

                                                  vy = 0;

                                                  break;

 

                              }

                    }

 

                    private function onEnterFrame(e:Event):void

                    {

                              ch1.x += vx;

                              ch1.y += vy;

                    }

 

          }

}

Refactored conditionals to switches - they are faster.

Inspiring
March 15, 2013

There are no event listeners added to keyboard and enter frame in this code. Where do you add event listeners?

Ned Murphy
Ned MurphyCorrect answer
Legend
March 14, 2013

You have two functions named onKeyDown.  You need to have different names for all functions in the class.

Participant
March 14, 2013

so under the first function called:

// Movement

function onKeyDown(event:KeyboardEvent):void

I can call or declare this function called:

function onKeyUp(event:KeyboardEvent):void

===============OR=====================

under the second function called: fuction onKeyDown(event:KeyboardEvent):void

I can replace that with: fuction onKeyUp(event:KeyboardEvent):void

Ned Murphy
Legend
March 14, 2013

Yes, you could do that.  Try it and see which is the correct one to do it with if you cannot reason it out.