PDA

View Full Version : Problem with X and Y Movement



Insane Knux
July 10th, 2005, 02:19 AM
I have this player, and you use the left and right arrow keys to control him.
Well he has a running animation and this is the code:

onClipEvent(enterFrame){
if (Key.isDown(Key.RIGHT)){
_x += 10;
this.gotoAndStop(2);
} else {
this.gotoAndStop(1);
}
}
onClipEvent(enterFrame){
if (Key.isDown(Key.LEFT)){
_x -= 10;
this.gotoAndStop(3);
}
}
Well, on the Left Key Press, when you press it, he goes left, but he doesnt play his animation... why?
Any help would be awesome! ;)

Blackspirit
July 10th, 2005, 04:00 AM
Two enterframes on one object = bad :D
Use of if else statement = good




onClipEvent(enterFrame)
{
if (Key.isDown(Key.RIGHT)){
_x += 10;
this.gotoAndStop(2);
} else if (Key.isDown(Key.LEFT)){
_x -= 10;
this.gotoAndStop(3);
} else {
this.gotoAndStop(1);
}
}

NiñoScript
July 10th, 2005, 09:06 AM
Yep, Blackspirit's code is ok

things that you have to remember:
• Never use more than one "onEnterFrame"
• be carefull with your ifs, in your code, when the right key was not pressed it changed the frame, but when the left was, it changed to another, u get it? when u press the left, you also werent pressing the right key

Insane Knux
July 10th, 2005, 06:47 PM
Holy crap guys, thanks! You guys rock! ;)