Mini-tutorial on event flow and transitions

A user sent me an email asking about how to structure a sequence of game events. I’m posting the answer here for everyone’s benefit.

Suppose you have a sprite moving over the screen. When the sprite reaches a character, you want the character to jump to avoid the sprite, if it matches a particular condition. In the examples below, the sprite is going to be a sheep.

  • First, there’s no “best” way to do this, a lot of it is up to how you want to structure your game and code.

  • Corona is primarily an event-driven system. When you tap on a button, an event triggers and calls an event listener – a function that is called when the event happens. Thinking in terms of what events happen when is the key to a responsive application.

  • For every line of code, think of when it is going to be run. If a piece of code is in main.lua and not inside a function, for example, it is run once, before the first frame. This is what is typically called the global “scope”. Another possible scope could be a local function, if the code is inside a function. For example, if you add the following:
    [lua]start:addEventListener( “tap”, start )[/lua]
    at the global “scope” in main.lua, it is called exactly once, before the first frame is displayed. If it is inside a function, then it is called when the function is called. Identify the scope of your code.

  • It is often very helpful to use print statements to debug what is going on. The print statements go into the console output, so you need to launch the app Corona Terminal instead of Corona Simulator
    [lua]print("sheep x = " … sheep.x )[/lua]
    This will print a message when it runs. If it’s in a listener function and doesn’t print, then the listener wasn’t called!

  • Ok, so how would you make a sheep jump. A transition is one way to do this. To transition the sheep up, you set the y location. The key to making the sheep go back down is adding a listener for the end of the “up” transition. This is the params.onComplete as described in the docs. That would be a separate function. In fact, if you make functions for the sheep move transition, the sheep up transition, and the sheep down transition, your code will get a lot shorter. The code might look something like (and I haven’t tested this, so it might have a few errors):
    [lua] function sheepLanded()
    – score!!!
    end
    function sheepDown()
    – causes sheep to move down. when transition is done, calls sheepLanded
    transition.to( sheep, { time = 1000, y = 100, onComplete=sheepLanded } )
    end
    function sheepJump()
    – causes sheep to move up. when transition is done, calls sheepDown
    transition.to( sheep, { time = 1000, y = -100, onComplete=sheepDown } )
    end
    function gameOver()
    – display game over screen
    end
    function testScore()
    – The sheepjump variable could be set in some other function, or it could be calculated here.

– if color matches, then make the sheep jump, else game over
if sheepjump == true then
sheepJump()
else
gameOver()
end
end
function sheepMove()
transition.to( sheep, { time = 4000 - (level * 300), x = -105, onComplete=testScore } )
end[/lua]

As you can see, using several chained transitions and event listeners allows actions to proceed in sequence. If you like, you can set the transition easing function to have smoother animation.

There’s a similar user-contributed example on our code section, here.

[import]uid: 54 topic_id: 3199 reply_id: 303199[/import]

Great job Eric. I am sure that will some people new to corona big time. [import]uid: 5712 topic_id: 3199 reply_id: 9408[/import]