If I have multiple objects on top of each other and I only want one of them to react to a tap event, how do I do it without having to specify which object it is that responds to it? (say, only the top one responds, or even a random one from the “pile”, I don’t mind. [import]uid: 106739 topic_id: 20325 reply_id: 320325[/import]
This code puts a stack of 10 objects in the middle of the screen and will only respond where the id of the object equals ‘stacknum’. You could set stacknum to be randomly generated each time the stack is touched.
An alternative to this method would be to only add the event listener to the first object in the stack, and when that is triggered, remove the event listener from that object and add a new one to the next one up.
[lua]local objects = {}
local stacknum = 1
local touchStack = function (event)
local obj = event.target
local id = obj.id
if event.phase == “ended” and id == stacknum then
– Put what to do when object is touched here
print (“Object No. “…id…” touched”)
stacknum = stacknum + 1
end
end
for a = 1, 10, 1 do
local obj = display.newImageRect(“object.png”,100,100)
obj.x = 240; obj.y = 160
obj.id = a
obj:addEventListener(“touch”,touchStack)
objects[a] = obj
end
[import]uid: 93133 topic_id: 20325 reply_id: 79409[/import]
Hmm. I think I may have worded this in the wrong way.
There is no actual “pile” or “stack” of objects, it’s just a word I used for multiple objects being on top of eachother.
So what I have, is pedestrians moving along a sidewalk. They are following a loop, so some of them overlap (walk over) other pedestrians. I can tap pedestrians to make them cross.
If there are, say, 3 pedestrians on top of eachother and I tap, they all cross.
How would I avoid that? Nothing predetermines that they will be on top of eachother, so there is no “array” to go through to pick one of them off the “pile”
Does that make sense? [import]uid: 106739 topic_id: 20325 reply_id: 79413[/import]
Assume that if there are (say) 3 overlapping you do not ALWAYS want the topmost one to react.
When a tap is detected, you need to know which others are close enough to have been ‘possibles’
Record the x co-ordinate of the tapped item. Thats your ground zero.
Create an array.
Loop through all of the pedestrians, comparing the x co-ordinate with the x co-ordinate of the tapped one.
If the difference is less than (say) 20 , they are closeby.
So add any close ones to the array.
(This will also add the original… thats fine, no need to add any code to say 'if this one is NOT the tapped one)
Now you have (n) objects in your array, you just need to pick a random number between 1 and (n) using math.random
Then make object(n) do what you want to happen.
Sounds like a lot of work, but its very quick.
Im not sure why 3 items would respond to one tap if they have individual listeners.
If that is the issue, you need a way to stop the others from handling the tap.
The tap event will pass x,y co-ords.
So you could have a variable that stores the x/y of the last tap.
Initialise this to 0/0
If an object is called upon to respond to a tap, it checks the x/y co-ord against the last value in the variable.
If it is the same, do nothing.
If it is different, do the business, then change the xy of the variable.
That way, only one object reacts to a tap of 100,200
You have to tap again on a different point to make anything else happen.
[import]uid: 108660 topic_id: 20325 reply_id: 79417[/import]
Solved it by adding
[lua]return true[/lua]
before the ‘end’ of the tap method
[import]uid: 106739 topic_id: 20325 reply_id: 79449[/import]