The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumHow do I link pressing of a key to a specific movement?

5 4842
Wyuen
offline
Wyuen
374 posts
Nomad

For example, I want to move a character to the right. So I press the right key.

BUT: When I press the right key, I want to the character to move. So it has to play a moveclip of him walking. How do I link the walking to the pressing of the right key? And he also has to move at a certain speed to the right.

Here is what I've got:

[On the character]

onClipEvent(enterFrame) {

if (Key.isDOWN (Key.RIGHT) )
gotoAndPlay(2)

}
}

The 2 is the frame inside the character. Inside the character there are 2 frames, 1 is jumping and 2 is walking.

The problem is however, when I press the right arrow key, it does play frame 2, but it keeps on playing, even when I've released the arrow key.

Whats wrong? and how do I fix it?

  • 5 Replies
gaboloth
offline
gaboloth
1,612 posts
Peasant

First, writing code on the clips is generally a bad idea, but it's not a big deal if these are your first tries to make a game.
However, you should have a frame for standing still too, and then tell flash to play that when no key is being pressed. If you have a single if statement, you can just put an else, if not write something like
If ( ! (Key.isDown ( key.RIGHT)) && ! (Key.isDown ( key.LEFT)) )
{
gotoAndPlay(standingstill)
}

took95
offline
took95
438 posts
Nomad

go into your character and name the frame where it is facing to the right " right " and the frame where it is facing left " left " wihtout quote marks
then put this as the code:

onClipEvent(enterFrame)
{
if (Key.isDown(Key.RIGHT))
{
//whatever you have to move the chararcter then this
_root.character.gotoAndStop("right&quot
}
if(Key.isDown(Key.LEFT))
{
_root.character.gotoAndStop("left&quot
}


make sure that the first frame, when the character isnt moving has the code
stop();
on it by right clickign it and clicking actions then make sure that you change the word character in my code to whatever youhave your character instance named. then if you make a symbol of him/her/it facing right and make a motion tween of it swinging its leg in step or something it will play it since it is in the frame named right or left or standing or whatever you name it as long as you change the names accordingly. hope this helps!
took95
offline
took95
438 posts
Nomad

ooops, i see, i see, my comp didnt load everything and i rushed into typing, so after you have your code and if it works then after the if brackets put this

else
{
_root.character.gotoAndStop("1&quot
}

that way it will go back to frame 1 once you have let go, since you haveit say enterFrame at the top(it does right?) it will keep checking over and over to see if that button is down and if it isnt it wont play 2, but if you add more frames/positions you may need it different because it could say that since your not pressing right it has to go to 1, even if you have something forleft, but maybe not...hope THIS helps P
plasmafish
offline
plasmafish
252 posts
Nomad

Instead of having a frame of the character walking to the left and right, just have one frame called run. When your direction is right use _xscale = 100; for moving right, and when your direction is left use _xscale = -100; for moving left.

And for walking/running speed, if Key.isDown(Key.RIGHT), speed++, if Key.isDown(Key.LEFT), speed--.

Then I also have a else if that describes what happens when the character slows down.

else if (speed<1 && speed>-1 && !jumping && !falling) {
// if speed<1 & >-1
speed = 0;
// speed is set to 0
character.gotoAndStop("idle&quot;
}

On some of the enemies I have, _xscale has to be larger than 100 to give a proportional look to them.

I am moving the root._x of my movie opposite of the speed variable and this is how the character travels around the game.

atlantin
offline
atlantin
1 posts
Nomad

nikc

Showing 1-5 of 5