Touch doesn't register for a second spawned object?

So I was trying to make a game where you tap one object and that object disappears and then a new one shows up. After tapping the first object that spawns and it disappearing, the second object spawns, as planned, but it does not register when I “touch” it. Here’s the code I think is causing it, I just don’t know why or how.

function spawnobj()

obj = display.newImageRect(“obj.png”, 175, 175)

    obj.y = 800

    obj.x = Random

    physics.addBody(obj, “dynamic”, {density=0.01, friction=0.0, bounce=01.0, radius=40});

end

local function removeobj()

    obj:removeSelf();

    spawnobj()

end

spawnobj()

obj:addEventListener(“touch”, removeobj);

Thanks!

Hi @legoboy5352,

Try this

local rand = math.random function spawnobj() obj = display.newImageRect("obj.png", 175, 175) obj.y = 100 obj.x = rand(20, 300) physics.addBody(obj, "static", {density=0.01, friction=0.0, bounce=01.0, radius=40}); local function removeobj(event) if(event.phase == "began") then obj:removeSelf(); --spawnobj() timer.performWithDelay( 1000, spawnobj, 1)-- if you want to set 1 second delay end end obj:addEventListener("touch", removeobj); end spawnobj()

Good Luck!

burhan

Thanks! It works as intended to now! But I’m still unsure why it didn’t work, can you please explain what I did wrong?

Its basically the sequence the code runs.

After you call spawnobj() it spawned the obj as intended.

Then its goes to your last line which adds you event listener.

Then when you touch the obj it calls the removeobj function.

This function remove your obj then spawn your obj.

But this time the last sentence ( addEventListener) is not call again because it ends with the end statement of the spawnobj().

Make sense?

One way for you to check if you have problem is to use the print statement and see in your console.

e.g.

print(“CheckPoint  1, 2, 3 …”)  – place it if you want to see the function has been called.

or 

print(“Function spawnobj() called.”)  – place it inside the function code block.

You can also check other things like,

print("Check object location XY : ", obj.x,  obj.y  )

burhan

Hi @legoboy5352,

Try this

local rand = math.random function spawnobj() obj = display.newImageRect("obj.png", 175, 175) obj.y = 100 obj.x = rand(20, 300) physics.addBody(obj, "static", {density=0.01, friction=0.0, bounce=01.0, radius=40}); local function removeobj(event) if(event.phase == "began") then obj:removeSelf(); --spawnobj() timer.performWithDelay( 1000, spawnobj, 1)-- if you want to set 1 second delay end end obj:addEventListener("touch", removeobj); end spawnobj()

Good Luck!

burhan

Thanks! It works as intended to now! But I’m still unsure why it didn’t work, can you please explain what I did wrong?

Its basically the sequence the code runs.

After you call spawnobj() it spawned the obj as intended.

Then its goes to your last line which adds you event listener.

Then when you touch the obj it calls the removeobj function.

This function remove your obj then spawn your obj.

But this time the last sentence ( addEventListener) is not call again because it ends with the end statement of the spawnobj().

Make sense?

One way for you to check if you have problem is to use the print statement and see in your console.

e.g.

print(“CheckPoint  1, 2, 3 …”)  – place it if you want to see the function has been called.

or 

print(“Function spawnobj() called.”)  – place it inside the function code block.

You can also check other things like,

print("Check object location XY : ", obj.x,  obj.y  )

burhan