COCOS2D-X quickly gets familiar with LUA details

Source: Internet
Author: User

I quickly introduced some features of Lua. If you want to learn Lua quickly, we recommend that you read it first.

1. {}, constructor

{A = 1, B = 2} is equivalent to {["a"] = 1, ["B"] = 2}

{"X", "y", "z"} is equivalent to {[1] = "x", [2] = "y", [3] = "z "}

 


If you really need to use 0 as an array:

A = {[0] = "x", "y", "z"} -- not recommended

 


Ii. Use local variables whenever possible to facilitate the Garbage Collector to release memory

If it is a global variable, you can consider the following when using it locally:

Local temA =

 


Use local variables to save the value of global variables. If the subsequent function changes the value of A, you can consider this to speed up access to the current domain.

 

 

 

Iii. control statements

 


1. if then else

 


2. while


[Cpp
While true or false do
-- Todo
End

While true or false do
-- Todo
End

 

3. repeat-


[Cpp]
Repeat
-- Todo
Until true or false

Repeat
-- Todo
Until true or false

 

4.

 


For start, variable, step (default 1 if not written) do

-- Todo

End

 


5. iterator


[Cpp]
For k, v in ipairs (a) do
-- K is an index.
-- V is the corresponding value.
End
 
For k in pairs (a) do
-- K is an index.
End

For k, v in ipairs (a) do
-- K is an index.
-- V is the corresponding value.
End

For k in pairs (a) do
-- K is an index.
End


It can be used to reverse table:


[Cpp]
RevA = {}
For k, v in ipairs (a) do
RevA [v] = k
End

RevA = {}
For k, v in ipairs (a) do
RevA [v] = k
End

 

Note: Other switches and do-while do not exist.

 


5. return and break can only be placed before the end, else, and until of a block.


[Cpp]
Function func ()
Local x = 0
X = 1
Print (x)
Return -- I want to jump out here. This is wrong.
If (x> 1) then
X = 10
End
End

Function func ()
Local x = 0
X = 1
Print (x)
Return -- I want to jump out here. This is wrong.
If (x> 1) then
X = 10
End
End


Modify:


[Cpp]
Function func ()
Local x = 0
X = 1
Print (x)
Do
Return -- This is correct. You can execute return
End
If (x> 1) then
X = 10
End
End

Function func ()
Local x = 0
X = 1
Print (x)
Do
Return -- This is correct. You can execute return
End
If (x> 1) then
X = 10
End
End

 

Vi. function. A function can return multiple values.

For example:


[Cpp]
Function func ()
Return "a", "B" -- two values are returned.
End

Function func ()
Return "a", "B" -- two values are returned.
End

 

 


1. If the function is the last one of the expressions, the function retains as many return values as possible to assign values to variables.


[Cpp]
X = func () -- x = "a", return value "B" don't
X, y = func () -- x = "a", y = "B"
X, y, z = 1, func () -- x = 1, y = "a", z = "B" note that func is the last expression
X, y, z = func () -- x = "a", y = "B", z = nil returned values are not enough to be supplemented with nil

X = func () -- x = "a", return value "B" don't
X, y = func () -- x = "a", y = "B"
X, y, z = 1, func () -- x = 1, y = "a", z = "B" note that func is the last expression
X, y, z = func () -- x = "a", y = "B", z = nil returned values are not enough to be supplemented with nil

 

 


2. if the function is not the last expression, only one value is returned.


[Cpp]
X, y, z = func (), 1 -- x = "a", y = 1, z = nil

X, y, z = func (), 1 -- x = "a", y = 1, z = nil

 

3. In the table constructor, if the function is at the last position of the constructor, all values are returned; otherwise, a value is returned.


[Cpp]
T = {func ()} -- t = {"a", "B "}
T = {func (), 2} -- t = {"a", 2}

T = {func ()} -- t = {"a", "B "}
T = {func (), 2} -- t = {"a", 2}


4. After return, the function returns all values.

 


5. If the function is in (), only one value is returned.


[Cpp]
Print (func () -- output a B
Print (func () -- the function is put in (), and output

Print (func () -- output a B
Print (func () -- the function is put in (), and output

 

6. The real parameter of a function can be a variable-length parameter. If the real parameter of a function contains both a fixed parameter and a variable-length parameter, the variable-length parameter must be placed behind the fixed parameter. Please note that... Usage

 


7. How to Write descriptive function arguments


[Cpp]
// OC code:
-(Void) createRect :( float) X y :( float) Y width :( float) Width height :( float) Height
{
// TODO...
}

// OC code:
-(Void) createRect :( float) X y :( float) Y width :( float) Width height :( float) Height
{
// TODO...
}

 

Sometimes there are many function parameters and you want to use the parameter described in oc. Although lua is not supported, it can be implemented using table.


[Cpp]
Function createRect (rectOptions)
If type (rectOptions. title )~ = "String" then
Error "need title"
End
End
 
CreateRect ({x = 0, y = 0, width = 200, height = 300, title = "hello sunny "})

Function createRect (rectOptions)
If type (rectOptions. title )~ = "String" then
Error "need title"
End
End

CreateRect ({x = 0, y = 0, width = 200, height = 300, title = "hello sunny "})

 

Tip: when there is only one table for the function arguments, do not (), so you can write it:


[Cpp]
CreateRect {x = 0, y = 0, width = 200, height = 300, title = "hello sunny "}

CreateRect {x = 0, y = 0, width = 200, height = 300, title = "hello sunny "}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.