《cocos2d-x遊戲開發》—— lua學習總結(一)數組的使用

來源:互聯網
上載者:User

標籤:get   使用   資料   os   表   for   

在lua中,數組是用table來實現的。

 

1、數組的定義:

self.itemArrays = {};   --作為數組來使用的表itemArrays

 

2、 數組插入一條資料:

local showItemSprite =  CCSprite:create();  --建立一個精靈對象showItemSprite

table.insert(self.itemArrays,table.getn(self.itemArrays)+1,showItemSprite);  --將showItemSprite插入itemArrays這張表(也就是我們的數組)

                                         --插入的位置為itemArrays的長度+1,也就是表的末尾                                                           

擷取數組(表)長度的函數:table.getn(數組名);               

 

3、遍曆這個數組:

 

local length = table.getn(self.itemArrays);

for i = 1 ,length do
  local itemSprite = self.itemArrays[i]; --通過下標i去取出數組裡面對應的元素(lua中表的下標是從1開始的)
  if itemSprite ~= nil then

  --對數組裡的元素itemSprite進行操作

  end
end

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.