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.
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.
5 | 4842 |
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?
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)
}
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"
}
if(Key.isDown(Key.LEFT))
{
_root.character.gotoAndStop("left"
}
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!
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"
}
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"
}
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.
You must be logged in to post a reply!