zoukankan      html  css  js  c++  java
  • 《cocos2d-x游戏开发》—— lua学习总结(一)数组的使用

    在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

    4、删除数组中的元素

    table.remove(数组名,下标);

  • 相关阅读:
    UOJ309 UNR #2 排兵布阵
    BZOJ4860: [Beijing2017]树的难题
    CQOI2017 部分题解
    SDOI2017 Round1 Day2 题解
    记SCOI2017
    BZOJ3810: [Coci2015]Stanovi
    BZOJ4785: [Zjoi2017]树状数组
    「ZJOI2007」「LuoguP1169」棋盘制作(并查集
    「LuoguP4147」 玉蟾宫(并查集
    「LuoguP1402」 酒店之王(最大流
  • 原文地址:https://www.cnblogs.com/Liyang1992/p/3810850.html
Copyright © 2011-2022 走看看