1

我会尽量简洁地解决我的问题。

首先,文件是:

block.lua
base.lua
main.lua

在 block.lua 中,我创建了一个块,添加了碰撞检测和清理代码。在 base.lua 中,我创建了一个由 4 列和 10 行组成的基础。一共40个街区。在 main.lua 中,我创建了 4 个由 base.class 制成的基础。

游戏开始后一切正常。我移除了基地并在第 2 层再次调用它们。他们自己创造良好但是当敌人再次被摧毁并且基地要重建时,我得到:

array index 1 is beyond array bounds:1..1 

——一直到——

array index 800 is beyond array bounds:1..159 

然后它将创建基地并继续直到敌人被摧毁并从以下开始再次执行相同的操作:

array index 800 is beyond array bounds:1..159 

——一直到——

array index 4000 is beyond array bounds:1..159

终端将我指向 block.lua 第 23 行

blockGroup:insert(blockNum,self.block)

现在我在课堂上看不到任何问题,我已经查看并搜索了几个小时,但都无济于事。我真的很感激能帮助我在这里指导我。我曾尝试重写“清理”等,但没有任何乐趣。我在那里留下了一些注释掉的部分,并删除了一些不相关的东西。我在相关代码下方发布:

--MAIN.LUA--

function gameOver()
  Runtime:removeEventListener("enterFrame", onEnterFrame)
  Runtime:removeEventListener("enterFrame", movePlayer)

  layers:removeSelf()
  layers = nil

  enemyCount = 0

   for i = 1,#allEnemys do
     timer.cancel(allEnemys[i].clock)
     Runtime:removeEventListener( "enterFrame", allEnemys[i] )
     display.remove(allEnemys[i].image)
     allEnemys[i].image=nil
   end
     allEnemys=nil

cleanupBlocks()
   end
----------------------------------------------------------------------
              -- LEVEL UP --
 ----------------------------------------------------------------------
function levelUp(level)
enemyCount = 0

local enemys = require("modules.enemy")
    if allEnemys ~= nil then
      for i = 1,#allEnemys do
           timer.cancel(allEnemys[i].clock)
           Runtime:removeEventListener( "enterFrame", allEnemys[i] )
           display.remove(allEnemys[i].image)
           allEnemys[i].image=nil
      end
    end
           allEnemys=nil
 cleanupBlocks()
 levels()

 end
----------------------------------------------------------------------
              -- LEVELS --
----------------------------------------------------------------------
function levels(level)

function createInvader(x, y, row)
  for j = 1, 2 do
    for i = 1, 2 do
      if allEnemys == nil then
       allEnemys = {}
      else
       enemysCount=#allEnemys
      end
      allEnemys[#allEnemys + 1] = enemys:new()
      allEnemys[#allEnemys ]:init(i * 60, j * 70 + 70,j) 
      allEnemys[#allEnemys ]:start()
    end
  end
end
createInvader()

--[[function createBases1()

local base = require("modules.base")
  for i = 1, 4 do
    base:new()
    base:init(i * 180 - 130, 850)
  end
end  ]]--

createBases()
end

--块.LUA--

local block = {}
local block_mt = { __index = block}
local scene = scene
local blockGroup = display.newGroup()
local blockNum = 0


function block:new() -- constructor
local group = {}
return setmetatable( group, block_mt )
end

function block:init(xloc,yloc) --initializer
-- Create attributes

self.block = display.newRect( xloc,yloc,10,10)
self.block:setFillColor ( 2, 255, 14  )

blockNum = blockNum + 1
blockGroup:insert(blockNum,self.block)

local blockCollisionFilter = { categoryBits = 128, maskBits = 387}
physics.addBody( self.block,  "static", {filter = blockCollisionFilter})
self.count = 1

end
function cleanupBlocks()

--[[ print(blockNum, blockGroup.numChildren)
for i=1,blockGroup.numChildren do
blockGroup[1]:removeSelf()
blockGroup[1] = nil
end  ]]--
print(blockNum, blockGroup.numChildren)
while blockGroup.numChildren>0 do

  display.remove(blockGroup[1])
  blockGroup[1]=nil
end

end

function block:start()
---  Create Listeneres
 self.block:addEventListener( "collision", self )
 scene:addEventListener('base_block_event', self)
end

return block

--BASE.LUA--

local base = {}
local base_mt = { __index = base}
local scene = scene

local block = require("modules.block")

function base:new() -- constructor
local group = {}
return setmetatable( group, base_mt )
end

function base:init(xloc, yloc) --initializer
-- Create attributes
local base

for j = 1, 4 do
    for i = 1, 10 do
        base = block:new()
        base:init(xloc+i * 10,yloc+j * 10)
        base:start()

    end
end

end

return base
4

1 回答 1

0

我看你用

blockNum = blockNum + 1
blockGroup:insert(blockNum,self.block)

尝试使用

blockGroup:insert(self.block)

只是看看你是否仍然得到那个错误。

于 2013-05-05T20:21:57.203 回答