I am trying to create an attack that spawns multiple walls with small loopholes through them, it is currently a work in progress. I am having trouble removing the objects. After 50 seconds, there is a timer that is supposed to remove the walls, but I keep getting this error at the 50 second mark:
ERROR: Runtime error attack.lua:73: attempt to perform arithmetic on field 'x' (a nil value) stack traceback: attack.lua:73: in function '?' ?: in function \<?:172\>
This is my code:
math.randomseed(os.time()) local lifetime = 50000 local attack = {} function attack.horizontalAttack(blockadeNumber, spaceApart, speed, change) local function moveBlockades(self) --Line 73 if self.x + 15 \> 0 then self.x = self.x - speed end end local function removeBlockades(self) display.remove(self) self:removeEventListener("enterFrame", self) end for i = 1, blockadeNumber do local blockadeBottom = display.newRect(right + (i \* spaceApart), bottom, 15, 200 + (i \* change)) blockadeBottom.anchorY = 1 blockadeBottom:setFillColor(0.56, 0.13, 0.94) --physics.addBody(blockadeBottom, "dynamic", {isSensor = true}) local blockadeTop = display.newRect(blockadeBottom.x, top, 15, 200 - (i \* change)) blockadeTop.anchorY = 0 blockadeTop:setFillColor(0.56, 0.13, 0.94) --physics.addBody(blockadeTop, "dynamic", {isSensor = true}) blockadeBottom.enterFrame = moveBlockades Runtime:addEventListener("enterFrame", blockadeBottom) blockadeTop.enterFrame = moveBlockades Runtime:addEventListener("enterFrame", blockadeTop) blockadeTop.timer = removeBlockades timer.performWithDelay(lifetime, blockadeTop) blockadeBottom.timer = removeBlockades timer.performWithDelay(lifetime, blockadeBottom) end end return attack
I think it is a problem with the removal of the objects and their eventListeners. Any help would be greatly appreciated. Thank you.