1
\$\begingroup\$

First off, I'm using the current publicly available build, version 2011.704

I'm building a platformer, and have a character that runs along and jumps when the screen is tapped. While jumping, the animation code has him assume a svelte jumping pose, and upon the detection of a collision with the ground, he returns to running. All of this happens.

The problem is that there is this strange gap of time, about 1/2 a second by the feel of it, where my character sits on the first frame of the run animation after landing, before it actually starts playing. This leads me to believe that the problem is somewhere between the "prepare" step of loading up a sprite set's animation sequence and the "play" step. Thanks in advance for any help :).

My code for when my character lands is as follows:

local function collisionHandler ( event )
    if (event.object1.myName == "character") and (event.object2.type == "terrain") then
        inAir = false
        characterInstance:prepare( "run" ) -- TODO: time between prepare and play is curiously long...
        characterInstance:play()
    end
end
\$\endgroup\$
2
  • \$\begingroup\$ Just a blind shot: what's this prepare statement doing? Is it loading the animation? If so this could explain a delay. \$\endgroup\$ Jun 18, 2012 at 23:17
  • \$\begingroup\$ The prepare statement sets the current frame of the animation to the first frame of the sequence specified as an argument, and pauses the animation there. \$\endgroup\$ Jun 19, 2012 at 0:17

1 Answer 1

1
\$\begingroup\$

Aha! Figured it out! The problem has to do with the collision event being thrown multiple times as things balance out physics wise (as it should be). My collision handler being what it was, the "prepare" step would keep getting called, setting the animation to the first frame, until the collision events stopped firing.

My fix was to write a little state machine for the character that guards against resetting the state if the program requests a state that is already active, thus preventing the animation from continuously being set to the first frame.

It looks like this:

characterInstance.stateMachine = {
    currentState = "State not set",
    ["set"] = function ( newState )
                        if characterInstance.stateMachine.currentState ~= newState then
                            characterInstance.stateMachine[newState]()
                        end
                    end,
    ["running"] = function ()
                        characterInstance:prepare( "run" )
                        characterInstance:play()
                        characterInstance.stateMachine.currentState = "running"
                    end,
    ["jumping"] = function ()
                        characterInstance:prepare( "jump" )
                        characterInstance:play()
                        characterInstance.stateMachine.currentState = "jumping"
                    end,
    ["attacking"] = function ()
                        characterInstance:prepare( "attack" )
                        characterInstance:play()
                        characterInstance.stateMachine.currentState = "attacking"
                    end
}

An example of setting the state: characterInstance.stateMachine["set"]( "jumping" )

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .