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(数组名,下标);

  • 相关阅读:
    中序遍历
    二叉树前序遍历
    A Real Stewart
    走遍美国 听写
    2016-12-12——2016-12-16友邻
    英语百日听力
    6.2分鱼问题两种解法
    Bootstrap组件1
    Bootstrap图标及另一个好用图标网站介绍
    Bootstrap全局CSS样式之图片
  • 原文地址:https://www.cnblogs.com/Liyang1992/p/3810850.html
Copyright © 2011-2022 走看看