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

What are functions?

Functions can carry out a procedure or compute and return values. We can make a function call as a statement, or we can use it as an expression. You can also use object methods as functions. You have learned that functions can be variables. A table can use these variables to store them as properties.

Functions are the most important means of abstraction in Lua. One function that we have used many times is print. In the following example, the print function is being told to execute one piece of data—the "My favorite number is 8" string:

print("My favorite number is 8") -- My favorite number is 8

Another way of saying this is that print is being called with one argument. The print function is only one of the many built-in functions that Lua has, but almost any program you write will involve you defining your own functions.

Defining a function

When trying to define a function, you have to give it a name that you can call out to when you want to return a value. You then have to create a statement or statement block of what the value will output and then apply end to your function after you have finished defining it. Here is an example:

function myName()
  print("My name is Jane.")
end

myName()  -- My name is Jane.

Notice that the function name is myName, and it is used to call out what's inside the print("My name is Jane.") function definition.

An extension on defining a function is as follows:

function myName(Name)
  print("My name is " .. Name .. ".")
end

myName("Jane")  -- My name is Jane.
myName("Cory")  -- My name is Cory.
myName("Diane")  -- My name is Diane.

The new myName function has one argument using the Name variable. The "My name is " string is concatenated with Name and then a period as the printed result. When the function is called, we used three different names as an argument, and the result is printed with a new customized name for each line.

More display functions

In Corona, you can change the appearance of the status bar on your device. This is a one-line setting in your code that takes effect once you launch your application. You can change the appearance of your status bar using the display.setStatusBar(mode) method. This hides or changes the appearance of the status bar on iOS devices (iPad, iPhone, and iPod Touch) and Android 2.x devices. Android 3.x devices are not supported.

The argument mode should be one of the following:

  • display.HiddenStatusBar:To hide the status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.HiddenStatusBar)

    In the following screenshot, you can see that the status bar is hidden:

    More display functions
  • display.DefaultStatusBar: To show the default status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.DefaultStatusBar)

    The code will display the default status bar, as shown in the following screenshot:

    More display functions
  • display.TranslucentStatusBar: To show the translucent status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.TranslucentStatusBar)

    The translucent status bar will look like the one shown in the following screenshot:

    More display functions
  • display.DarkStatusBar: To show the dark status bar, you can use the following line at the beginning of your code:
    display.setStatusBar(display.DarkStatusBar)

    The following screenshot is of the dark status bar:

    More display functions

Content size properties

When you want to obtain the display information on your device, you can use the content size properties to return the values. These properties are as follows:

  • display.contentWidth: This returns the original width of the content in pixels. This will default to the screen width.
  • display.contentHeight: This returns the original height of the content in pixels. This will default to the screen height.
  • display.viewableContentWidth: This is a read-only property that contains the width of the viewable screen area in pixels, within the coordinate system of the original content. Accessing this property will display how the content is viewed, whether you're in the portrait or landscape mode. Here is an example:
    print(display.viewableContentWidth)
  • display.viewableContentHeight: This is a read-only property that contains the height of the viewable screen area in pixels, within the coordinate system of the original content. Accessing this property will display how the content is viewed, whether you're in the portrait or landscape mode. Here is an example:
    print(display.viewableContentHeight)
  • display.statusBarHeight: This is a read-only property that represents the height of the status bar in pixels (only valid on iOS devices). Here is an example:
    print(display.statusBarHeight)