Cocos2d-x Game Development -- Lua learning Summary (1) use of Arrays

Source: Internet
Author: User
Tags array definition

In Lua, arrays are implemented using tables.


1. array definition:

Self. itemarrays ={}; -- The itemarrays table used as an array


2. Insert a data entry into the array:

Local showitemsprite = ccsprite: Create (); -- creates a showitemsprite object.

Table. insert (self. itemarrays, table. getn (self. itemarrays) + 1, showitemsprite); -- insert showitemsprite into the itemarrays table (our array)

-- The insert position is the length of itemarrays plus 1, that is, the end of the table.

Function for obtaining the length of an array (table): Table. getn (array name );


3. traverse this array:


Local length = table. getn (self. itemarrays );

For I = 1, length do
Local itemsprite = self. itemarrays [I]; -- uses subscript I to retrieve the corresponding elements in the array (the subscript of the table in Lua starts from 1)
If itemsprite ~ = Nil then

-- Operate the itemsprite element in the logarithm Group



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