local widget = require("widget") local spawnedObjects = {} local function myTouchListener( event ) if(event.phase == "began") then -- if (event.target ~= nil) then if (target ~= nil) then timer.performWithDelay(1, function() event.target:removeSelf() end) timer.performWithDelay(1, function() event.target = nil end) end end return true -- returning true "prevents touch propagation to underlying objects" end local function spawnBubble(bubbleSize) local bubble = display.newCircle(0, 0, bubbleSize) bubble:setFillColor(.5, .5, .5) bubble.radius = bubbleSize bubble.x = 400 bubble.y = 600 spawnedObjects[#spawnedObjects+1] = bubble spawnedObjects[#spawnedObjects]:addEventListener( "touch", myTouchListener ) end local function circleGrow() for i = 1,#spawnedObjects do if (spawnedObjects[i] ~= nil) then local storedRadius = spawnedObjects[i].radius local storedX = spawnedObjects[i].x local storedY = spawnedObjects[i].y storedRadius = storedRadius + 1 print(spawnedObjects[i].radius) spawnedObjects[i]:removeSelf() -- spawnedObjects[i] = nil spawnedObjects[i] = display.newCircle(storedX, storedY, storedRadius) spawnedObjects[i].radius = storedRadius spawnedObjects[i]:setFillColor(.5, .5, .5) spawnedObjects[i]:addEventListener( "touch", myTouchListener ) end end end spawnBubble(100) timer.performWithDelay( 100, circleGrow, 0)
My problem is a simple-ish one, but I cannot seem to figure out to solution. I have the above code, and I am trying to make a sample game, where you have many growing circles, or bubbles, and they disappear on touch. However, something is not working. it keeps reporting that target == nil. (line 10). I do not know why this is, but if anyone could help me I would be very grateful. I can make the circles, and have them increase their radii by 1 ten times every second, but I cannot make them do anything on touch. I used to have it so that there was no if statement surrounding lines 11-12, but then there would be an error. I have also tried another if statement (seen but commented out on line 9). This would check if event.target==nil, which I think should work. So, why does it think that target==nil? Why does it not execute the lines of code in the if statement? it seems to me that the conditional is true, that the target is not a nil value.