Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
上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 values to them. What if you have a variable that has multiple values attached to it? How does Lua differentiate them? We'll use the Corona terminal so that 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 refer to chunks in this exercise. The unit of execution in Lua is called a chunk. A chunk is a block that is executed sequentially. Follow these steps on getting started with Lua:

If you remember, in the previous chapter, you learned how to create your 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. Add in the while loop:
    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 that you see the Corona SDK 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 shown here:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    25
    10
    

What just happened?

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

The if statement compares i < 20, where i equals 11 at this point and uses another local x variable 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.