Skip to main content
Participant
March 5, 2013
Question

Problems with animating character movement

  • March 5, 2013
  • 1 reply
  • 749 views

Hi!

I am pretty new to as3, and so I have encountered a problem that I cannot solve by my self.

I have searched other forums and this to see if I can find a solution, but have not found one yet.

I'm making a game where this character is supposed to move left, right,up and down. And so I've animated her walking in all different directions,

and also made the corresponding animations play whenever the right buttons are pushed.

Before I go any further, here's the code so far:

stop();

character.stop();

var vx=0;

var vy=0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);

stage.addEventListener(Event.ENTER_FRAME, onEnterThisFrame);

function onKeyPress(e:KeyboardEvent):void{

if(e.keyCode==Keyboard.DOWN){

vy=3;

character.gotoAndPlay("frontWalk");///character is a movieclip with different animated movieclips inside it.

}

else if(e.keyCode==Keyboard.UP){

character.gotoAndPlay("backWalk");

vy=-3;

}

else if(e.keyCode==Keyboard.RIGHT){

character.gotoAndPlay("leftWalk");

vx=3;

}

else if(e.keyCode==Keyboard.LEFT){

character.gotoAndPlay("rightWalk");

vx=-3;

}

}

function onKeyRelease(e:KeyboardEvent):void{

if(e.keyCode==Keyboard.DOWN){

character.gotoAndStop("frontStand");

vy=0;

}

else if(e.keyCode==Keyboard.UP){

character.gotoAndStop("backStand");

vy=0;

}

else if(e.keyCode==Keyboard.RIGHT){

character.gotoAndStop("leftStand");

vx=0;

}

else if(e.keyCode==Keyboard.LEFT){

character.gotoAndStop("rightStand");

vx=0;

}

}

function onEnterThisFrame(e:Event):void{

character.x+=vx;

character.y+=vy;

}

The code works fine if I hold a arrow key down, release it and wait for a second before i push the arrow next key. But if I don't wait a while before I push the next key, it will move the character with the standing still frame for a second and then animate the right walking animation.

Its like, the code can't catch up or something.

I hope I am making sense.

I would highly appreciate any help on this matter.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 5, 2013

you should use an enterframe loop to update your character.  use your keyboardevent listener functions to assign variables that inform you enterframe loop listener function how to update your character.

Anzie356Author
Participant
March 5, 2013

could you be more specific as to what you mean?

I though I already did it with the onEnterThisFrame function?

Nabren
Inspiring
March 5, 2013

Use a dictionary to track key states and then do all your updating in the enterFrameHandler. Example:

import flash.events.KeyboardEvent;

import flash.utils.Dictionary;

import flash.events.Event;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

var keyState:Dictionary = new Dictionary();

function keyDownHandler(e:KeyboardEvent):void

{

          keyState[e.keyCode] = true;

}

function keyUpHandler(e:KeyboardEvent):void

{

          keyState[e.keyCode] = false;

}

function enterFrameListener(e:Event):void

{

          if (keyState[Keyboard.LEFT])

          {

                    // Do left

          }

 

          if (keyState[Keyboard.RIGHT])

          {

                    // Do right

          }

 

          if (keyState[Keyboard.UP])

          {

                    // Do up

          }

 

          if (keyState[Keyboard.DOWN])

          {

                    // Do down

          }

}

You will solve your issue and also be able to hold multiple keys down at the same time. If you don't want this - just make them else if instead with your priority:

import flash.events.KeyboardEvent;

import flash.utils.Dictionary;

import flash.events.Event;

import flash.ui.Keyboard;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

var keyState:Dictionary = new Dictionary();

function keyDownHandler(e:KeyboardEvent):void

{

          keyState[e.keyCode] = true;

}

function keyUpHandler(e:KeyboardEvent):void

{

          keyState[e.keyCode] = false;

}

function enterFrameListener(e:Event):void

{

          if (keyState[Keyboard.LEFT])

          {

                    // Do left

          }

 

  else if (keyState[Keyboard.RIGHT])

          {

                    // Do right

          }

 

  else if (keyState[Keyboard.UP])

          {

                    // Do up

          }

 

  else if (keyState[Keyboard.DOWN])

          {

                    // Do down

          }

}