Ive got this function that spawns objects and a timer that replays this function 5 times (so 5 of the same object show up).
When i touch one of the objects, i want to remove the object and leave a 'splat' or a mark where it was removed. I've tried this but when there are two of the objects on the screen, the mark gets left on the other object and not the one that I touched.
(the boxes are moving around)
So how can i display a mark on the coordinates of each box after it is removed (even when there are multiple boxes on the screen).
Thanks in advance :)
Heres my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | local box_x local box_y function box:touch(e) if (e.phase == "ended") then --coordinates of box box_x = box.x box_y = box.y --calls the function that holds the instructions whenTouch(self) end end box:addEventListener("touch", box) function whenTouch(obj) --displays the mark or 'splat' at the box's coordinates mark = display.newImage("ball.png", box_x, box_y) --removes box obj:removeSelf() end |
you need to spawn the objects as different images
1 2 3 4 5 6 7 8 9 | image = {} imgCount = 1 function spawnImage() image[imgCount] = display.newImage(....) imgCount = imgCount + 1 end timer.performWithDelay( 2000, spawnImage, 5 ) |
@shaunv,
Let's see where the error in your code could be...
in the touch function, you are using self and box, I presume that of the two boxes that you have, when you click on the first one, the second one gets removed, is that right?
It is because the variable 'box' now refers to the second box where as there is no handle to the first anymore.
to rectify it,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | local box_x, box_y local box1, box2 function onTouch(event) local phase = event.phase local target = event.target if "ended" == phase then target:setReferencePoint(display.TopLeftReferencePoint) box_x = target.x box_y = target.y target:removeSelf() mark = display.newImage("ball.png", box_x, box_y) end end box1 = display.newWhatever() box2 = display.newWhatever() box1:addEventListener("touch", onTouch) box2:addEventListener("touch", onTouch) |
thanks man :)
worked just fine