# Animating a sprite in AS3. help needed

**URL:** <https://forum.kirupa.com/t/animating-a-sprite-in-as3-help-needed/260501>\
**Category:** Uncategorized\
**Created:** [May 16, 2008, 9:05am UTC](https://forum.kirupa.com/t/animating-a-sprite-in-as3-help-needed/260501 "2008-05-16T09:05:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![highway81](https://avatars.discourse-cdn.com/v4/letter/h/8dc957/32.png) [@highway81](https://forum.kirupa.com/u/highway81)\
**Post date:** [May 16, 2008, 9:05am UTC](https://forum.kirupa.com/t/animating-a-sprite-in-as3-help-needed/260501/1 "2008-05-16T09:05:00Z")

</div>

hi,

i know this will be pretty simple for most of you but i am a beginner. i am trying to animate a sprite to walk when i press the arrow key. when the arrow key is pressed the sprite moves across the stage but does not animate (simulate walking) until the button is released.this is the code:

//define variables, false by default  
var keyLeft:Boolean;  
var keyRight:Boolean;

//add keyboard event listeners to the stage that listen for keyboard key presses  
stage.addEventListener(KeyboardEvent.KEY\_DOWN,keyDownFunction);  
stage.addEventListener(KeyboardEvent.KEY\_UP,keyUpFunction);

//give you motion whenever keyLeft or keyRight is set to true;  
addEventListener(Event.ENTER\_FRAME,moveObject);

//when key is pressed down  
function keyDownFunction(evt:KeyboardEvent) {  
if (evt.keyCode == 37) {  
keyLeft = true;  
}  
if (evt.keyCode == 39) {  
keyRight = true;

}  
}

function keyUpFunction(evt:KeyboardEvent) {  
if (evt.keyCode == 37) {  
keyLeft = false;  
}  
if (evt.keyCode == 39) {  
keyRight = false;

}  
}

//comes into play whenver keyLeft or keyRight is set to true  
function moveObject(evt:Event) {  
if (keyLeft) {

```
ryu_mc.x -= 5;
ryu_mc.gotoAndStop('walking')

```

} else if (keyRight) {

ryu\_mc.x += 5;  
ryu\_mc.gotoAndStop(“walking”);

}  
}

any help would be appreciated. cheers.

---

<div class="post-metadata">

**Author:** ![adnan794](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@adnan794](https://forum.kirupa.com/u/adnan794)\
**Post date:** [May 16, 2008, 11:27am UTC](https://forum.kirupa.com/t/animating-a-sprite-in-as3-help-needed/260501/2 "2008-05-16T11:27:53Z")

</div>

Okay what is at the walking frame is there any animation there? because at the moment your just moving ur sprite in the horizontal direction and then stopping it on frame labled walking

---

<div class="post-metadata">

**Author:** ![tkmk](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tkmk](https://forum.kirupa.com/u/tkmk)\
**Post date:** [May 18, 2008, 2:40pm UTC](https://forum.kirupa.com/t/animating-a-sprite-in-as3-help-needed/260501/3 "2008-05-18T14:40:09Z")

</div>

I personally think as2 would be the answer to your problem.
