-
AS2 - player movement problem
Hello.
I need help in improving the code to move the character in my game (AS2).
The problem is that I would like move the player character only in four directions (left, right, up, down)
For movement I'm using a script:
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
_root.player._x += 5;
}
if(Key.isDown(Key.LEFT)){
_root.player._x -= 5;
}
if(Key.isDown(Key.UP)){
_root.player._y -= 5;
}
if(Key.isDown(Key.DOWN)){
_root.player._y += 5;
}
}
If I press two buttons (up and left or up and right or down and left or down and right) at the same time it moves diagonally.
I would like to block the possibility of pressing two buttons. Let nothing happen when two buttons are pressed.
Thx for help
-
Client Software Programmer
I would put else before the if statement so it picks closest one to the top pressed every frame per second
if(Key.isDown(Key.RIGHT)){
_root.player._x += 5;
}else if(Key.isDown(Key.LEFT)){
_root.player._x -= 5;
}else if(Key.isDown(Key.UP)){
_root.player._y -= 5;
}else if(Key.isDown(Key.DOWN)){
_root.player._y += 5;
}
}
[/PHP]
-
.
Hi
Use a switch
Look up as2 switch
-
Originally Posted by Alloy Bacon
I would put else before the if statement so it picks closest one to the top pressed every frame per second
if(Key.isDown(Key.RIGHT)){
_root.player._x += 5;
}else if(Key.isDown(Key.LEFT)){
_root.player._x -= 5;
}else if(Key.isDown(Key.UP)){
_root.player._y -= 5;
}else if(Key.isDown(Key.DOWN)){
_root.player._y += 5;
}
}
[/PHP]
Thank you very much. It helped. I'm owed you a large beer :-)
-
Client Software Programmer
I hope it worked if anything send it to fruitbeard he taught me the scripts: https://www.paypal.com/donate/?token...GB&locale.x=GB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|