View Full Version : Flash Game: Problem with walking.
NarcoticGlow
06-22-2007, 04:00 AM
:confused: Hey, I'm pretty new to all this Flash, but i made a short game, and I'm having problems with the scripting.
When I press and release the arrow key for him to move he walks normally. When I hold the key down, my character moves but looks like he is stuck in the same frame. Can you help me. I can't find any advice in any Forum. It's been picking on me all week. PLEASE HELP ME!!!
Here is a sample of my coding:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x = this._x-12;
gotoAndPlay(1);
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+12;
gotoAndPlay(6);
}
}onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x = this._x-12;
gotoAndPlay(1);
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x = this._x+12;
gotoAndPlay(6);
}
}
fauzira
06-22-2007, 08:31 AM
I think you must put the walking animation inside movieclip.
ex:
Character_Mc (Level0)=> WalkingAnimationLeft_Mc (Level1)
Character_Mc (Level0)=> WalkingAnimationRight_Mc (Level1)
Put the script inside MovieClip Character_Mc and change gotoAndPlay to gotoAndStop
When you hold onto the left key, it'll constantly play frame 1, because the expression "Key.isDown(Key.LEFT)" will always be true and evaluate "gotoAndPlay(1)". So your character will be stuck at frame 1.
You need a couple variables to keep track of what your current state and previous state are.
Example: If you press "left", there are two cases:
1. If your previous state was not "left", (either standing or moving right) you play the first frame of "left". In this case it's frame 1.
2. If your previous state was already "left", you simply play the next frame with the .nextFrame function.
There's also the case of looping back to the first frame of "left".
The attachment below uses just one single "enterFrame" sequence and puts all the frames (facing right, facing left, and standing) into one movie clip, so it does differ a bit from your approach.
orange gold
06-27-2007, 05:45 PM
nooooooooooooooo!!!!!!!
the simple way is to make the animation inside of the symbol "doubble click on it" then on the first frame say
if (Key.isDown(Key.LEFT)) {
man._x += -5;
}
then inside the symbol have it stop then say
if (Key.isDown(Key.LEFT)) {
gotoAndPlay("walking left") // walking left is the frame name you have for him walking left
}
zombibubonik
06-27-2007, 08:55 PM
I had ths problem, too, my solution was to make a seperate movie clip for each of the characters action (ie. manStanding, manRunning, manJumping) and put them each in a frame of another movie clip (say, manMC), then the code goes as follows;
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x = this._x-12;
gotoAndStop(2); //Where 2 is the frame that contains the running animation
} else if (Key.isDown(Key.RIGHT)) {
this._x = this._x+12;
gotoAndStop(2);
this._xscale = -100; //To make him face the opposite direction
} else {
gotoAndStop(1); //To go back to 1 if no button is being pressed, where 1 is the frame containing the manStanding animation
}
Ferret
06-27-2007, 09:26 PM
I agree with zombibubonik (kind of) and wyat. Zombie's code just needs wyat's idea.
so here are both ideas together, with my idea:
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x = this._x-12;
if(idle == true){
gotoAndPlay("RUNNING");
idle = false;
//Where "RUNNING" is the frame that contains the running animation
}
} else if (Key.isDown(Key.RIGHT)) {
this._x = this._x+12;
if(idle == true){
idle = false;
gotoAndPlay("RUNNING");
this._xscale = -100; //To make him face the opposite direction
}
} else {
idle = true;
gotoAndPlay("IDLE"); //To go back to "IDLE" if no button is being pressed,
//where "IDLE" is the frame containing the manStanding animation. and if
//you don't have an animation for him standing then change it to
//gotoAndStop().
}
About zombie's idea...
Instead of making different movie clips, make their animations all on one MC timeline, so all the animations run into each other. Then at the last frame of one animation (like running) put gotoAndPlay("RUNNING"). That way it's a loop and you don't have a bunch of junk movie clips. And it may look funny to you to have a string in gotoAndPlay(), but it's called a frame label.
You should always use frame labels for this sort of thing. In your man MC, click on the beginning frame for each action he does and then in the properties box you'll see a frame label box. Type in a name for that frame. I use all caps because it's just a habit for me. You can name it whatever. It's useful for if you want to change an animation. Like if you want to make the running animation just one frame longer, your whole MC timeline will be thrown off by 1 frame and your gotoAndStop() commands will be wrong. With the labels it will always go to the beginning of the animation you want. ;) Good luck with the game!
orange gold
07-07-2007, 07:16 AM
thats what i said....
vigorousjammer
01-02-2008, 07:28 AM
ok, now I have a question relating to all of this...
I know we're currently only talking about movement on the X axis...
but let's say I was working on a top-down game, moving in 4 directions...
if I wanted there to be several idle states, one for up, down, left and right... i know i would have to have 4 seperate standing animations inside my MC, but how would I tell it what frame to goto if a key was just released?
bitCulture
01-13-2009, 07:55 AM
jammer, the way i dealt with that was by making an else statement for the if key is down condition.
for example:
if (Key.isDown(Key.WHATEVER))
{
//do some stuff
}
else
{
//do other stuff
}
bluemagica
01-13-2009, 09:13 AM
jammer please don't resurrect a old thread, instead make your own for your own question, that helps us to answer better!
And yes to solve that, you can use an if else ladder, or you can use better names for your variables/frames.
like gotoAndStop(currentPose+"_idle");
assume currentPose is a variable which will hold if character is facing "up", "down" etc!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.