The code below illustrates my problem. I can spawn a moving bullet in the centre of the screen with a tap. When it goes outside the screen, it gets removed. This works fine until I fire off a couple of bullets in quick succession. Then I get an error that reads: ‘main.lua:26: attempt to index field ‘?’ (a nil value)’.
Any enlightenment would be appreciated.
BULLETSPEED = 15 local bullets = {} local function addBullet(event) --spawns circle at screen centre, travelling in direction of tap, adds it to "bullets" table local index = #bullets + 1 bullets[index] = display.newCircle(display.contentCenterX, display.contentCenterY, 10) local dY = event.y - display.contentCenterY local dX = event.x - display.contentCenterX local bulletAngle = math.atan2(dY, dX) bullets[index].velocity = {math.cos(bulletAngle) \* BULLETSPEED, math.sin(bulletAngle) \* BULLETSPEED} end local function frameUpdate(event) -- destroy out-of-bounds bullets for i = #bullets, 1, -1 do if bullets[i].x \< 0 or bullets[i].y \< 0 or bullets[i].y \> display.contentHeight or bullets[i].x \> display.contentWidth then bullets[i]:removeSelf() bullets[i] = nil end end -- move bullets for i = #bullets, 1, -1 do bullets[i].x = bullets[i].x + bullets[i].velocity[1] -- line 26 bullets[i].y = bullets[i].y + bullets[i].velocity[2] end end Runtime:addEventListener("tap", addBullet) Runtime:addEventListener("enterFrame", frameUpdate)