How To Make Touch Trigger To Delete An Object? Newbiest Newbie Needs Help!

Hi everybody

I’ll start by saying  I’m da biggest newbie/noob you’ve probably ever seen. So please, no “use shfkshjs to do askdbfksbf it’s so easy” :slight_smile: And now, here’s the problem:

local myImage = display.newImage( "example1.png" ) myImage.x = 610 myImage.y = -10000 myImage.rotation = 0 physics.addBody( myImage, { density=0.1, friction=2.0, bounce=0.0 } ) local function touchListener( event )     print( "Touched" )     media.playEventSound( "boom.mp3" ) end myImage:addEventListener( "touch", touchListener )  

First problem: I have this in my code. When myImage is touched, it clearly says “touched” in terminal and then plays the “boom.mp3”. But when I built the app just to see if it really works, no sound comes when I touch myImage (sgs3 GT-I9300 Android 4.1.2) :frowning:

Second problem: How to make myImage to remove itself when touched and change to another picture, let’s say “example2.png”.

Thaaaanks :)  :slight_smile:

  1. Bonus points for this being your first post and yet you used code formatting.  Thanks!  

  2. I’ve modified your code to delete the object after the touch ends (look for EFM in code):

    local myImage = display.newImage( “example1.png” ) myImage.x = 610 myImage.y = -10000 myImage.rotation = 0 physics.addBody( myImage, { density=0.1, friction=2.0, bounce=0.0 } ) – EFM notice the change in how this function (now a method) is defined – (self is the object that was touched ) local function touchListener( self, event ) local phase = event.phase --EFM print( "Touched: ", phase )-- EFM – EFM all actions will happen on finger lift or event cancel if( phase == “ended” or phase == “cancelled” ) then     media.playEventSound( “boom.mp3” ) – EFM After a short pause remove this object – Tip: Never directly remove an object while it is being processed – during a collision or touch. Always delay that deletion by 1 clock. timer.performWithDelay(1, function() self:removeSelf() end ) end return true – Stops event propagation to layers below this object end – EFM Define the ‘myImage’ default touch hander as the method ‘touchListner()’ myImage.touch = touchListener --EFM The following can be thought of as ‘myImage’ adds “touch” event listener and the event is sent to – the ‘myImage’ default touch handler (if defined) myImage:addEventListener( “touch”, myImage )  

  3. Sorry, I’ll let someone else answer the sound question.

Wow thank you, didn’t expect an answer so throughly and well done! Going to try this first thing in the morning and I hope this works (though I’m sure it will :))

  1. Bonus points for this being your first post and yet you used code formatting.  Thanks!  

  2. I’ve modified your code to delete the object after the touch ends (look for EFM in code):

    local myImage = display.newImage( “example1.png” ) myImage.x = 610 myImage.y = -10000 myImage.rotation = 0 physics.addBody( myImage, { density=0.1, friction=2.0, bounce=0.0 } ) – EFM notice the change in how this function (now a method) is defined – (self is the object that was touched ) local function touchListener( self, event ) local phase = event.phase --EFM print( "Touched: ", phase )-- EFM – EFM all actions will happen on finger lift or event cancel if( phase == “ended” or phase == “cancelled” ) then     media.playEventSound( “boom.mp3” ) – EFM After a short pause remove this object – Tip: Never directly remove an object while it is being processed – during a collision or touch. Always delay that deletion by 1 clock. timer.performWithDelay(1, function() self:removeSelf() end ) end return true – Stops event propagation to layers below this object end – EFM Define the ‘myImage’ default touch hander as the method ‘touchListner()’ myImage.touch = touchListener --EFM The following can be thought of as ‘myImage’ adds “touch” event listener and the event is sent to – the ‘myImage’ default touch handler (if defined) myImage:addEventListener( “touch”, myImage )  

  3. Sorry, I’ll let someone else answer the sound question.

Wow thank you, didn’t expect an answer so throughly and well done! Going to try this first thing in the morning and I hope this works (though I’m sure it will :))