[help]How to know what object is touched in a group

Hi! I am new in lua programming and now I have a dude with the event

I have targets moving down on the screen, they are in a group called Dianas and I want know what diana is touched to remove it. here is the code

function generar( event )

score = score + 1

scoreTxt.text = score

local posX = math.random( 1 , 2 )

local posIzq = 75

local posDch = display.contentWidth - 75

if posX == 1 then

diana = display.newImage( “diana.png” , posIzq ,  -100)

else

diana = display.newImage( “diana.png” , posDch ,  -100)

end

dianas:insert( diana)

totalElementos:insert( dianas )

dianas:toFront( )

scoreTxt:toFront( )

end

generarDianas = timer.performWithDelay( 300, generar, -1 )

function getDeltaTime()

local temp = system.getTimer() 

local dt = (temp- runtime) / (1000/30)  

runtime = temp  

return dt

end

function moverDianas( event )

local dt = getDeltaTime()

for i = dianas.numChildren,1,-1  do

if(dianas[i].y > display.contentHeight + 200) then

dianas:remove(dianas[i])

else

dianas[i].y = dianas[i].y + 15 * dt

end

end

end

Runtime:addEventListener(“enterFrame”, moverDianas )

function eliminarDiana( event )

if (event.phase == “began”) then

for i = dianas.numChildren,1,-1  do

if(dianas[i].) then

dianas:remove(dianas[i])

end

end

end

end

dianas:addEventListener( “touch”, eliminarDiana )

the problem is if I use diana to use the event, corona says it is nil and brake the game. Thx for your answers

Why don’t you attach a listener to each individual display object if you need to determine that? instead of attaching

the listener to the whole group. You can do both and I would expect both to get called during a touch event.

If you must attach to the group only for whatever reason. You can always get the event.x event.y and do a collision test with

all of the display objects in the group. 

yeah, I didnt understand the events a lot, now yes, yesterday I could do it doing this. Thx a lot!

PD: there are anyway to call a function each TIME (but the TIME can change each X secs)

In order to have time delayed call back functions you might want to use timer.performWithDelay, only as you may know

already it only supports a static time. In our app we use this function. When we need to change the timing we use timer.cancel and then setup the timer.performWithDelay using the new time.

Unfortunately, one of the other issues you can run across when using the timer.performWithDelay is that if you app is ever suspended

by the user, when / if it is resumed, the timers all get cancelled by Corona and you need to track/restart them in code somewhere.

Thacks for your answer, I do this, and works fine:

function generar( event ) if (tiempoGenerar \> 200) then if (velocidad \< 22) then velocidad = velocidad + 2 end tiempoGenerar = tiempoGenerar - 200 end if (velocidad == 17 and tiempoGenerar == 200) then velocidad = 25 tiempoGenerar = 150 end print(velocidad) print(tiempoGenerar) local posX = math.random( 1 , 2 ) local posIzq = 75 local posDch = display.contentWidth - 75 if posX == 1 then diana = display.newImage( "diana.png" , posIzq , &nbsp;-100) diana:addEventListener( "touch", eliminarDiana ) else diana = display.newImage( "diana.png" , posDch , &nbsp;-100) diana:addEventListener( "touch", eliminarDiana ) end dianas:insert( diana ) totalElementos:insert( dianas ) dianas:toFront( ) scoreTxt:toFront( ) generarDianas = timer.performWithDelay( tiempoGenerar, generar, 1) end local generarDianas = timer.performWithDelay( 600, generar, 1)

I dont know if that I do its correct and the use of memory its nice. Thx for all and excuse me for my bad english

I thought you wanted the function to get called repetitively ? if that is the case you will need to add a -1 which repeats the function call infinitely at your given time interval and then you should do a timer.cancel before recreating the timer.

if you were only planing on calling it once initially and then once each time the generar function is called then it would probably work fine, Just be aware that simply recreating the timer will not stop the previous timer from executing, you need timer.cancel for that.

scoreTxt:toFront( )

if (generarDianas) then

  timer.cancel(generarDianas)

end 

generarDianas = timer.performWithDelay( tiempoGenerar, generar,  -** 1**)
end

Why don’t you attach a listener to each individual display object if you need to determine that? instead of attaching

the listener to the whole group. You can do both and I would expect both to get called during a touch event.

If you must attach to the group only for whatever reason. You can always get the event.x event.y and do a collision test with

all of the display objects in the group. 

yeah, I didnt understand the events a lot, now yes, yesterday I could do it doing this. Thx a lot!

PD: there are anyway to call a function each TIME (but the TIME can change each X secs)

In order to have time delayed call back functions you might want to use timer.performWithDelay, only as you may know

already it only supports a static time. In our app we use this function. When we need to change the timing we use timer.cancel and then setup the timer.performWithDelay using the new time.

Unfortunately, one of the other issues you can run across when using the timer.performWithDelay is that if you app is ever suspended

by the user, when / if it is resumed, the timers all get cancelled by Corona and you need to track/restart them in code somewhere.

Thacks for your answer, I do this, and works fine:

function generar( event ) if (tiempoGenerar \> 200) then if (velocidad \< 22) then velocidad = velocidad + 2 end tiempoGenerar = tiempoGenerar - 200 end if (velocidad == 17 and tiempoGenerar == 200) then velocidad = 25 tiempoGenerar = 150 end print(velocidad) print(tiempoGenerar) local posX = math.random( 1 , 2 ) local posIzq = 75 local posDch = display.contentWidth - 75 if posX == 1 then diana = display.newImage( "diana.png" , posIzq , &nbsp;-100) diana:addEventListener( "touch", eliminarDiana ) else diana = display.newImage( "diana.png" , posDch , &nbsp;-100) diana:addEventListener( "touch", eliminarDiana ) end dianas:insert( diana ) totalElementos:insert( dianas ) dianas:toFront( ) scoreTxt:toFront( ) generarDianas = timer.performWithDelay( tiempoGenerar, generar, 1) end local generarDianas = timer.performWithDelay( 600, generar, 1)

I dont know if that I do its correct and the use of memory its nice. Thx for all and excuse me for my bad english

I thought you wanted the function to get called repetitively ? if that is the case you will need to add a -1 which repeats the function call infinitely at your given time interval and then you should do a timer.cancel before recreating the timer.

if you were only planing on calling it once initially and then once each time the generar function is called then it would probably work fine, Just be aware that simply recreating the timer will not stop the previous timer from executing, you need timer.cancel for that.

scoreTxt:toFront( )

if (generarDianas) then

  timer.cancel(generarDianas)

end 

generarDianas = timer.performWithDelay( tiempoGenerar, generar,  -** 1**)
end