Question

I'm currently making a platform game in Flash CS6 using ActionScript 3. I've managed to setup basic sprites into a single symbol/object, using different frames.

Each sprite/action is itself a different symbol also (for example, there are 5 or so run to the right frames, so I merged them into a separate symbol. That symbol was then copied into the player symbol (or the main symbol).

I've managed to get my code to run the specific symbol/frames using the gotoAndPlay() command, however my problem is that even though I have a stop(); action at the end of the loop, it continues onto the next frame.

In this case, it is carrying on to run the first frame of the 'runLeft' animation, before reverting back to the correct 'runRight' animation. However it then keep switching between the two and flickering until I let go of the key.

Say I have a two run animations for both directions, both 5 frames long (1a, 2a, 1b etc). When I press the right arrow key, it will run frames;

1a, 1b, 1c, 1d, 1e

However, at 1e, I have a stop(); command, in theory thinking that since the case is still active it will loop back to the start. Instead, it does this;

1a, 1b, 1c, 1d, 1e, 2a, 1a, 2a, 1a.....so on so forth until I release the key. If I were to hold the left arrow key, it would go on to play the first frame of 'animation 3' (the next animation after it).

Here is the code I'm using, but I can't really see it being an issue with the DocumentMain.as code, but more of an issue with the symbol(s) themselves?

KeyDown(pressed) handler function;

switch(e.keyCode)
{
    case 37:    //Left Arrow
        _vx = -5;
        _player.gotoAndPlay("runLeft");
        break;

    case 38:    //Up Arrow
        _vy = -27;
        break;

    case 39:    //Right Arrow
        _vx = 5;
        _player.gotoAndPlay("runRight");
        break;

    default:
}

And the KeyUP handler function;

switch(e.keyCode)
{
    case 37:    //Left Arrow                
        _vx = 0;
        _player.gotoAndPlay("standLeft");
        break;

    case 39:    //Right Arrow
        _player.gotoAndPlay("standRight");
        _vx = 0;
        break;

    default:
}
Was it helpful?

Solution

There are a couple of issues here:

  1. The down key will get called repeatedly after a few seconds, so it will continually tell the movieClip to gotoAndPlay a certain frame, hence why you're getting the repeated 1a, 2a, 1a...etc at the end of your animation sequence. You need to only call the gotoAndPlay once on keyDown, i.e. perhaps use a boolean to track that:

    case 37:    //Left Arrow keydown
        _vx = -5;
        if (!isLeft) {
            isLeft = true;
            _player.gotoAndPlay("runLeft");
        }
        break;
    
    
    case 37:    //Left Arrow keyup
        _vx = 0;
        isLeft = false;
        _player.gotoAndPlay("standLeft");
        break;
    
  2. Instead of using stop(); on the movieClip frame, use i.e. gotoAndPlay('1a') so the animation will loop itself.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top