Oct 302011

An explanation of flags, what they are and how to use them in your apps.

Spending time in the New Users section of the Corona forums I have frequently found myself and others suggesting, “Just use a flag!” – which is often met with the online equivalent of a blank stare.

The purpose of this post is to give a brief explanation of flags as well as a simple example of how they might be used to better help your understanding of the process.

What Are Flags?

Flags can be used to tell us if the sound is on or off, if the user is touching the screen or not, if the hero in our game is on the right side of the screen or the left.

The sound on/off scenario is one of the more common circumstances in which we all find ourselves as we delve into app development and so my example will use this situation.

A Simple Flag (Plug and Play)

local bg = display.newRect( 0, 0, 320, 480 )

local sound = true

local function soundSwitch ()
sound = not sound
print (sound)
bg:addEventListener("tap", soundSwitch)

The above sample switches our variable (sound) between true and false when you tap on the background and prints the current value.

Why use flags?

Using the above example you could easily check whether sound was true or false during game play and accordingly.

Being able to quickly use

if sound == true

in a function whenever you need it is very useful.

You may also use a flag to check when a player is in a certain position on the screen – perhaps on the ground – and say if onGround == true then jump when the jump button is pressed, else do nothing. (To avoid being able to jump again in midair, etc.)

And that’s it!

Although this just scratches the surface of what flags can do I hope it will encourage new users to experiment with them – they are a simple but vital part of developing an application.

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!


  10 Responses to “Flags – What and Why”

  1. I hadn’t manually changed this over to lua yet – but now I have :) (Have to manually do it each time, it wont detect it.)

  2. 1
    local isThePluginWorking = system.getPluginStatus()
    if (isThePluginWorking == true) then

    Yep that was the problem. Does it auto detect the language or can you actually tell him to always use lua? :>
    Also it’s not working great in the main article (i can see the , but not the coloring)

  3. Ah OK – you didn’t close your code tags I’m guessing ;)

  4. Oh wow that didn’t work well.

    stupid plugin
  5. Apparently the plugin doesn’t like comments – only main posts >.< ' (Can do lua in main posts too, not just code blocks like above.) Wonder if it works in comments - if this is proper, it worked.


  6. local isThePluginWorking = system.getPluginStatus()
    if (isThePluginWorking == true) then

  7. Nice little tutorial to help starters :)
    You might aswell tell them how to use “or” instead of an if-else-statement!


    local player = display.newRect(display.contentWidth/2,display.contentHeight/2,30,30)

    local function global (event)

    – Move to left
    player.x = player.x-4
    – Check if x >= 0, if yes, move to the actual position, else stay at 0
    player.x = (player.x >= 0) and player.x or 0


    It’s like the if-else-shorthand in C and JavaScript ( statement ? true : false; )

    Though it makes the code a bit less readable ;)

    • Hey Chris,

      Thanks for that – certainly a good idea. Perhaps a tutorial on if statements (including and, or, else, etc,) would be a nice addition.

      Peach :)

  8. Hello Peach!

    Thank you for writing this article.

    It`s surely very useful and it is much more clear to ready your articles because you have and use a wonderful way to “speak”! Its awesome as it catch our attention easier.

    Keep up your great personality of being helpful as you are! ;)


 Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.