2015年4月1日 星期三

[Corona SDK] Powerful Table

In [Corona SDK] How to use Table?, we learn how to use Table.
Actually, we always encounter Table in Lua.
For example, we have mentioned that we can use t.name for t["name"].
It looks similar with those Lua APIs, right?

Yes, those functions are grouped by Table.
For example, for display related APIs, there exists a table named display.
In the table, there have different index keys which mapped to the correspondent functions.
So, the following two methods mean the same thing.
display.newText( "ee", display.contentCenterX, 80, native.systemFont, 20 )
display["newText"]( "ee", display.contentCenterX, 80, native.systemFont, 20 )

The Table is really powerful....

If we have many functions with related purposes, we can also group them with table.
It will look like the method of object in other languages.

If the function just needs to pass in some parameters.
Use t.name is fine.

What is we need to operate on the object itself?
For example, we draw a rectangle and rotate it.
local rec = display.newRect( 100, 100, 50, 50 )
rec.rotate(rec,45)
We need to pass in the object itself as parameter.
Lua provide a simple way for this:
local rec = display.newRect( 100, 100, 50, 50 )
rec:rotate(45)
That is, use colon ":" for dot ".". 
By doing so, we don't need to pass in the object itself.
It is merely a shortcut.

沒有留言:

張貼留言