New to this - character stuck in animation cycle

Hi all

I’ve recently been interested in Flash’s potential to make games, so thought I’d give it a try and see what happens. I’m not actually making a game here, just working through the process of controlling a character and changing animations, etc, that kind of thing. I’m completely new to this, so I’m doing this to learn actionscript in relation to games programming from the ground up using various tutorials and so on.

So I’ve set up a character to do 3 things at the moment: Stand, run, take a running jump.
I have 4 different movie clips, one for each animation, and one that stores al the animations under different frames. Frame 1 is labelled standing, 2 is labelled running, and 3 is labelled runjump.

By assigning action script to the movie clip that houses the animations, I’ve got so far, and here’s how it works.

  • Clip runs and player stands, setting their ‘state’ to “standing”.
  • Press Left or Right key, and it checks to make sure the players state is “standing” or already running, and if all is well, changes the clip to the frame that is storing the running animation and sets off in the right direction. Set the state to “running” aswell.
  • Let go of the left or right key and the player stands still again, setting the state back to “standing”

All that works great, but the problem is when it changes to the jump animation. Now at this stage, I don’t want it to actually jump anywhere, just change animation, then change back when it’s done. Here’s how it’s set to jump:

  • Player is running (say) left, holding down the Left key to make him run, then while holding onto Left, presses up to do a running jump.
  • Code checks that you are in fact running, all is well, so switches to jump animation.

However, as soon as you hit jump, the clip gets stuck in the animation and continues to jump over and over again, and no matter what keys you press, you can’t make him stop!

At the end of the jump animation, the state is set back to “standing” by the jump movie clip, so you wont see it in the below script. The ‘trace’ command says that that works fine, but the main movie clip doesn’t seem to be picking it up. So in theory, it should then just set the clip back to standing, right? But it doesn’t!

Now, I’m clearly missing something fundamental, but for the life of me can’t see what’s wrong here. No doubt a rookie mistake, but can anyone enlighten me so I can fix it and look out for it in the future?

The best explanation I can come up with is that the state needs to be set back to standing by the same movie clip. I.e. the jump movie clip is just setting a new variable by the same name. However to do this, how would the main movie clip know the jump animation has finished?

So if you’ve managed to sit through that saga this far, here’s the code. As you can see by the heavy commenting, this is a learning exercise for me rather than an actual game. The trace commands are there so I can see that the ‘state’ variable is changing ok, and it seems to be.

 
onClipEvent (load) {
// Set amount of pixels character moves at a time
 var speed = 20;
// _xscale controls character orientation (facing left or right)
 var scale = _xscale;
// set state to standing still
 var state = "standing";
}
// Character Movement
onClipEvent (enterFrame) {
// Run Left when you press the left key
 if (Key.isDown(Key.LEFT) && (state == "standing" || state == "running")) {
   this.gotoAndStop("running");
   state = "running";
   // Move player this many pixels
   _x -= speed;
   // Turn player to face to the left
   _xscale = -scale;
   trace(state);
 }
 
// Run Right when you press the left key
  if (Key.isDown(Key.RIGHT) && (state == "standing" || state == "running")) {
   state = "running";
   this.gotoAndStop("running");
   _x += speed;
   _xscale = scale;
   trace(state);
 }
// Running jump
 if (Key.isDown(Key.UP) && state == "running") {
   state = "jumping";
   this.gotoAndStop("runjump");
   trace(state);
   }
// If none of the controlling keys are down, goto stand animation
 
 if (!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) && state != "jumping") {
    this.gotoAndStop("stand");
   state = "standing";
  }
}

Any thoughts? I’m using Flash 8 by the way if that helps.