Leave a 'splat' after removal

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 :slight_smile:

Heres my code:

[code]
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 [import]uid: 86598 topic_id: 14393 reply_id: 314393[/import]

you need to spawn the objects as different images

[blockcode]
image = {}
imgCount = 1

function spawnImage()
image[imgCount] = display.newImage(…)
imgCount = imgCount + 1
end

timer.performWithDelay( 2000, spawnImage, 5 )
[/blockcode]

[import]uid: 7911 topic_id: 14393 reply_id: 53193[/import]

@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,

[lua]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)[/lua]
hope this resolves your issue,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 14393 reply_id: 53214[/import]

thanks man :slight_smile:
worked just fine [import]uid: 86598 topic_id: 14393 reply_id: 53337[/import]