COCOS2D-X quickly gets familiar with Lua details

Source: Internet
Author: User

Monkey original, reprinted. Reprinted Please note: Reprinted from cocos2d Development Network-cocos2dev.com, thank you!

Original address: http://www.cocos2dev.com /? P = 423

This blog post is mainly followed by "COCOS2D-X quickly familiar with Lua basic details" down to the next, so if it is cocos2dx to learn Lua, please take a look at the COCOS2D-X quickly familiar with Lua basic details of the problem, the main fast talk about some Lua features, so fast learning Lua, it is recommended to read it again.

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

while true or false do-- todoend

3. Repeat-

repeat-- todountil true or false

4.


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

-- Todo

End


5. iterator

For K, V in ipairs (a) do -- k is the index -- V is the corresponding valueendfor K in pairs (a) do -- k is the index end

It can be used to reverse table:

revA = {}for k,v in ipairs(a) dorevA[v] = kend

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.

Function func () Local x = 0x = 1 print (x) return -- it is wrong to jump out of here. If (x> 1) thenx = 10 endend

Modify:

Function func () Local x = 0x = 1 print (x) doreturn -- This is correct. You can execute returnendif (x> 1) thenx = 10 endend

Vi. function. A function can return multiple values.

For example:

Function func () Return "A", "B" -- returns two values: 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.

X = func () -- X = "A", return value "B" Do not X, Y = func () -- X = "A", y = "B" x, y, z = 1, func () -- x = 1, y = "A", Z = "B" note that func is the last x, y, z = func () of the expression () -- X = "A", y = "B", Z = nil return value is not enough to be supplemented with nil


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

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.

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.

Print (func () -- output a bprint (func () -- function is put in (), 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

// 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.

function createRect(rectOptions)if type(rectOptions.title) ~= "string" thenerror "need title"endendcreateRect({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:

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.