p.s. my code is rather big… and i dont know how to… simplyfy it in a nice clean version…sorry… .
event.target and background aren’t really the same. event.target is whatever the target of the event was. When you touch the screen, an event is dispatched to any objects under the touch that are listening for that event. For each object, an event table is created. This table has a number of properties including x, which is the x screen coordinate of the touch, and target which is a reference to the object receiving the particular event. Inside the listener function, you can access these properties, and their values, and use them desired. If you use the same function for different objects, and want to do different things when each is touched, you need to test which object was touched. This is why in the event handler I used:
if event.target == background and not img then -- stuff elseif event.target == img then -- stuff end
I was testing whether the background was being tapped or img was, and could take different steps accordingly. Notice as well that after checking event.target == background, I added “and not img”. This was just to see whether I had created img already or not, so that I wouldn’t end up creating it again after I tapped multiple times. I could have used two different handlers for background and img, maybe that would have been easier to understand. Also, be aware that what I did is just one way of doing things.
You might like to read this:
https://docs.coronalabs.com/guide/events/detectEvents/index.html
When you say you are having trouble with scope, do you mean this line,
img:addEventListener("tap", tapHandler)
being inside of tapHandler? Well, functions are allowed to call themselves. It’s quite useful for recursive algorithms.
yeah thats the line… and now i noticed that i didnt understand the NOT notation also…