Chapter 2. Learning Lua

Do you have an idea for an app? Would you like to create a game for players around the world? Before you can run, you have to learn to walk, and your next step in your mobile adventure is to learn how to use Lua.

This chapter will provide you with a starting foundation for how to use Lua with Corona SDK. Here are the topics that we will be covering in this chapter:

  • Using variables with Lua
  • Using the terminal
  • Expressions and operators
  • Making decisions
  • Loops, loops, and loops
  • More flexibility with tables
  • Using functions

Let's get started!

Using variables with Lua

With any mobile application, you will need a way to deal with information. Whether you need to store a player's level, name, or preferences, Lua provides you with a way to deal with information through variables.

Variables can store different types of information; the basic types are numbers, strings, and Boolean values:

  • Numbers can be anything from a quantity to the current player level. You can perform basic math operations on number variables, such as addition, subtraction, and more.
      myAge = 28;
  • A string is a set of characters, which can be anything including letters in the alphabet, numbers, and other characters. A string variable is easily identified by the double quotes surrounding the set of characters.
      myString = "My Mobile Adventure";
  • The third type of information is a Boolean value. Boolean values can only be either true or false. A great example of a Boolean value is the mute button. If the player has decided to mute your app, the value for the mute variable would be true. Otherwise, the variable is set to false.
      muteButton = true;

There are some rules with variables that we need to cover:

  • Lua is a dynamically typed language, and we do not need to specify the type of information that we are storing. Lua is smart enough to know when you want to store a string and when you want to store a Boolean value.
  • All variables are case sensitive. Therefore, myName and myname are different variables.
  • Lua has a list of reserved keywords that you cannot use when naming variables. Some of these reserved keywords are if, then, else, and do. For a full list of reserved keywords, visit www.lua.org/manual.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.2.122