How do i remove this image on touch?

So i have a function inside a function and i the function inside the first function had a touch listener that is supposed to remove an image but its not working…

Here’s the code…

 function removeArrow1(event) if event.phase == "began" then display.remove(arrow[aCounter]) print("bob" ) end end

the removeArrow1 function is supposed to remove the arrow when i touch it… but it doesnt … But it does print “bob”

Instead of using “arrow[aCounter]” as the reference to remove, use “event.target”.

Also, I suggest you make all of your functions local. Both the spawn and remove functions are global, and globals are almost always bad practice.

Best regards,

Brent

How do i do the event.target part? 

this is just a sample app that will be going into a bigger app…and in the bigger game i need them to be global …

When i do this

function removeArrow1(event) if event.phase == "began" then local remove = event.target.arrow display.remove(remove) print(event.target.arrow) end end

then in the console it prints “nil”.

event.target, not event.target.arrow.

Also, no bigger project “needs” globals. There is always a way to eliminate them, and you should try to do so.

Take care,

Brent

Ahh! Thanks Brent! I did this and it worked!

function removeArrow1(event) if event.phase == "began" then local remove = event.target display.remove(remove) print(event.target) end end

But how does it know what event target is? Is it because the function is inside the other function?

In any tap/touch handler function, event.target is the object that was tapped/touched.See here:

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

Ohh i understand now!! Thanks Again!

Instead of using “arrow[aCounter]” as the reference to remove, use “event.target”.

Also, I suggest you make all of your functions local. Both the spawn and remove functions are global, and globals are almost always bad practice.

Best regards,

Brent

How do i do the event.target part? 

this is just a sample app that will be going into a bigger app…and in the bigger game i need them to be global …

When i do this

function removeArrow1(event) if event.phase == "began" then local remove = event.target.arrow display.remove(remove) print(event.target.arrow) end end

then in the console it prints “nil”.

event.target, not event.target.arrow.

Also, no bigger project “needs” globals. There is always a way to eliminate them, and you should try to do so.

Take care,

Brent

Ahh! Thanks Brent! I did this and it worked!

function removeArrow1(event) if event.phase == "began" then local remove = event.target display.remove(remove) print(event.target) end end

But how does it know what event target is? Is it because the function is inside the other function?

In any tap/touch handler function, event.target is the object that was tapped/touched.See here:

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

Ohh i understand now!! Thanks Again!