Identifying Touched Objects

I’m having trouble figuring out the best way to associate touch events with specific objects in a group.

I have a touch function:

local function onTouch( event )
local t = event.target

that moves one of an array of objects around when dragged.

And elsewhere, I have a loop for altering the velocity of a set of objects:

for enemyID = 1, 8 do
enemy[enemyID].y = enemy[enemyID].y + enemyYV[enemyID]
enemyYV[enemyID] = enemyYV[enemyID] + .2
end

But I’m not sure how to alter the enemyYV[enemyID] value (velocity) in the touch function since I can’t tell how to identify which object in the enemy[enemyID] table is actually being touched.

I was thinking I’d need to loop through the table and match x.y coordinates of objects with x.y coordinates of the touch event, but that seems inelegant. Is there a better way?
[import]uid: 1560 topic_id: 325 reply_id: 300325[/import]

Maybe this will help clarify: This code creates a row of objects that slowly descends toward the bottom of the screen. Currently, you can select any one of those objects and drag it back up, but I can’t figure out how to access the Y velocity variable (enemyYV) inside the touch event code.

Any suggestions would be welcomed.

local setUp, enemy, enemyList, enemyImage, enemyValue, enemyXV, enemyYV, moveThem
display.setStatusBar( display.HiddenStatusBar )

enemyList = display.newGroup()
local function onTouch( event )
local t = event.target

local phase = event.phase
if “began” == phase then
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then

t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end

setUp = function()

enemy = {}
enemyImage = {“enemy.png”}
enemyValue = {}
enemyXV = {}
enemyYV = {}

for enemyID = 1, 8 do
enemy[enemyID] = display.newImage( enemyImage[1] )
enemyList:insert( enemy[enemyID] )
enemy[enemyID].x = -20 + (enemyID * 40)
enemy[enemyID].y = 70
enemyValue[enemyID] = 30
enemyYV[enemyID] = 1
enemyList[enemyID]:addEventListener( “touch”, onTouch )

end
end

setUp ()
moveThem = function()

for enemyID = 1, 8 do
enemy[enemyID].y = enemy[enemyID].y + enemyYV[enemyID]
–enemyYV[enemyID] = enemyYV[enemyID] + .2

end
end

main = function()

Runtime:addEventListener( “enterFrame”, moveThem )
end

main ()
[import]uid: 1560 topic_id: 325 reply_id: 506[/import]