function to destroy objects but only the object you are touching

Hey guys I was wondering how do you make it (for example in the game im making) have trees in the scene and when you tap them a certain number of times only the tree you were tapping with be destroyed and wood will be added to you inventory.

Thanks Tyler Jacobson

--This only the code that applys to this topic local woodamount = 0 local tree = display.newImage("tree1.png",300,226) local tree1 = display.newImage("tree1.png",390,226) local function chopdowntree (event) tree:removeSelf() tree1:removeSelf() end tree:addEventListener("tap",chopdowntree) tree1:addEventListener("tap",chopdowntree)

Hello Tyler,

Well, as a general overview, you’ll need to keep a count on each tree for how many times it has been tapped. Then, when the count is equal to a certain number, you can remove it.

Also, you should optimize your “chopdowntree” function. Instead of directly putting “tree” and “tree1” in there, you should use “event.target” to reference the tree which has been tapped. See this guide for details:

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Brent

Hello Tyler,

The best way to do this is to keep an array / LUA table of tree objects, and add a property on the tree item called touchedCount or something like that. Then like brent said above you use the event.target to get the tree object touched and add one to it something like this

( example code only

treeObject = event.target

treeObject.touchedCount = treeObject.touchedCount +1

if (treeObject.touchedCount > 3 ) then

  – call some add wood / lumber function here

  – remove tree from scene and memory

   timer.performWithDelay( 1000, treeObject.removeSelf() )

end

Thanks guys it took me awhile to figure it out but heres my code. I dont know if I did what you guys were thinking but I used this linkhttps://docs.coronalabs.com/guide/events/touchMultitouch/index.html the one Brent gave me. Thanks Tyler Jacobson :slight_smile:

local tree = display.newImage("tree1.png",300,226) local tree1 = display.newImage("tree1.png",390,226) local function myTapListener( event ) if ( event.numTaps == 2 ) then print( "object double-tapped = "..tostring(event.target) ) display.remove(event.target) woodamount = woodamount + math.random(1,5) print(woodamount) else return true end end tree1:addEventListener( "tap", myTapListener ) tree:addEventListener( "tap", myTapListener )

Hi Tyler,

Yes, you basically got it. :slight_smile: The next step IMHO would be to properly manage all of your “trees” in some sort of queue table, and ensure that when you remove them from the display (display.remove()), you also remove them from the table. That way you’ll handle memory properly.

Brent

Hello Tyler,

Well, as a general overview, you’ll need to keep a count on each tree for how many times it has been tapped. Then, when the count is equal to a certain number, you can remove it.

Also, you should optimize your “chopdowntree” function. Instead of directly putting “tree” and “tree1” in there, you should use “event.target” to reference the tree which has been tapped. See this guide for details:

https://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Brent

Hello Tyler,

The best way to do this is to keep an array / LUA table of tree objects, and add a property on the tree item called touchedCount or something like that. Then like brent said above you use the event.target to get the tree object touched and add one to it something like this

( example code only

treeObject = event.target

treeObject.touchedCount = treeObject.touchedCount +1

if (treeObject.touchedCount > 3 ) then

  – call some add wood / lumber function here

  – remove tree from scene and memory

   timer.performWithDelay( 1000, treeObject.removeSelf() )

end

Thanks guys it took me awhile to figure it out but heres my code. I dont know if I did what you guys were thinking but I used this linkhttps://docs.coronalabs.com/guide/events/touchMultitouch/index.html the one Brent gave me. Thanks Tyler Jacobson :slight_smile:

local tree = display.newImage("tree1.png",300,226) local tree1 = display.newImage("tree1.png",390,226) local function myTapListener( event ) if ( event.numTaps == 2 ) then print( "object double-tapped = "..tostring(event.target) ) display.remove(event.target) woodamount = woodamount + math.random(1,5) print(woodamount) else return true end end tree1:addEventListener( "tap", myTapListener ) tree:addEventListener( "tap", myTapListener )

Hi Tyler,

Yes, you basically got it. :slight_smile: The next step IMHO would be to properly manage all of your “trees” in some sort of queue table, and ensure that when you remove them from the display (display.remove()), you also remove them from the table. That way you’ll handle memory properly.

Brent