Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using arrow keys (keyboard)

New Here ,
Mar 13, 2013 Mar 13, 2013

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;

                }     

          

        }

                                    

    }       

}

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 13, 2013 Mar 13, 2013

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

Translate
LEGEND ,
Mar 13, 2013 Mar 13, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2013 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2013 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2013 Mar 15, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2013 Mar 15, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines