Corona SDK Mobile Game Development:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – printing values using blocks

Let's give it a shot and see how powerful a language Lua is. We're starting to get an idea of how variables work and what happens when you assign a value to it. What if you have a variable that has multiple values attached to it? How does Lua differentiate them? We'll be using the Corona Terminal so we can see the values outputted in the terminal box. Along the way, you'll pick up other programming techniques as you progress through this section. We will also be referring to chunks in this exercise. The unit of execution of Lua is called a chunk. A chunk is a sequence of statements, which are executed sequentially.

If you remember from the previous chapter, we learned how to create our own project folder and main.lua file for the Hello World application.

  1. Create a new project folder on your desktop and name it Variables.
  2. Open up your preferred text editor and save it as main.lua in your Variables project folder.
  3. Create the following variables:
    local x = 10 –- Local to the chunk
    local i = 1  -- Local to the chunk
  4. In the while loop, add the following code:
    while i<=x do    
      local x = i    -- Local to the "do" body
      print(x)       -- Will print out numbers 1 through 10 
      i = i + 1
    end
  5. Create an if statement that will represent another local body:
    if i < 20 then
      local x          -- Local to the "then" body
      x = 20
      print(x + 5)  -- 25
    else
      print(x)         -- This line will never execute since the above "then" body is already true
    end
    print(x)  -- 10
  6. Save your script.
  7. Launch the Corona Terminal. Make sure you see the CoronaSDK screen and a terminal window pop up.
  8. Navigate to your Variables project folder and open your main.lua file in the simulator. You will notice that the device in the simulator is blank, but if you look at your terminal window, there are some results from the code printed out as follows:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    25
    10
    

What just happened?

The first two variables that were created are local outside of each block of code. Notice in the beginning of the while loop, i <= x refers to the variables in lines 1 and 2. The statement local x = i inside the while loop is only local to the do body and is not the same as local x = 10. The while loop runs ten times and prints out a value that is incremented by 1 each time.

The if statement compares i < 20, where i equals 1 and uses another local x that is local to the then body. Since the statement is true, x equals 20 and prints out the value of x + 5 which is 25.

The very last line, print(x) is not attached to any of the blocks of code in the while loop or the if statement. Therefore, it refers to local x = 10 and prints out the value of 10 in the terminal window. This may seem confusing, but it's important to understand how local and global variables work in Lua.