hey guys i am stuck a little. want to to access the image or id in a table . when the food collide with the hero the hero would give a specific point to the food. i found this stack over flow but it address touch not collision http://stackoverflow.com/questions/16966784/corona-sdk-lua-how-to-evaluate-which-array-object-was-tapped this exactly what i want but in collsion form. here
foodTable = {“healthy food/strew”,“healthy food/milk”,“healthy food/keewee”,“healthy food/green”,“healthy food/greaps”,“healthy food/fish”,“healthy food/egg”,“healthy food/carrot”,“healthy food/bluebarrys”,“healthy food/banna”,“healthy food/apple”, “healthy food/Banana01”}
foods = {}
function spawnFoods()
ranNum = math.random(#foodTable)
foods[#foods + 1] = display.newImage( foodTable[ranNum]…".png" )
foods[#foods].x = math.random(-10, 300);
foods[#foods].y = -40;
foods[#foods].id = foodsNum
physics.addBody( foods[#foods], “dynamic”, { friction =1, bounce =0, density=0 } )
transition.to( foods[#foods], {time = math.random(2000, 8000), x = math.random(90, 100) , y = 480, onComplete = nil});
foods[#foods].name = “happy”
foods[#foods].isFixedRotation = true
total_food = 0
foodtimer= timer.performWithDelay( 1000, spawnFoods, total_food )
– hero movement on user’s drag
function movePlayer(event)
if event.phase == “began” then
moveX = event.x - hero.x;
elseif event.phase == “moved” then
hero.x = event.x - moveX;
end
if((hero.x - hero.width * 0.5) < 0) then
hero.x = hero.width * 0.5;
elseif((hero.x + hero.width * 0.5) > display.contentWidth) then
hero.x = display.contentWidth - hero.width * 0.5;
end
end
hero:addEventListener(“touch”, movePlayer);
local function onLocalCollision( self, event )
if event.other.name == “badfood” then
print ‘floor’
display.remove(event.other)
elseif event.other.name == “enemies01” then
display.remove(event.other)
elseif event.other.name == “happy” then
display.remove(event.other)
end
end
floor01.collision = onLocalCollision
floor01:addEventListener( “collision”, soldier )
— hero collision point / calories giver
local function onLocalCollision( self, event )
if event.other.name == “badfood” then
print ‘bad’
display.remove(event.other)
elseif event.other.name == “happy” then
display.remove(event.other)
end
end
hero.collision = onLocalCollision
hero:addEventListener( “collision”, soldier )is my code
hope this is good enough to answer my question. if any question please reply.
thanks
Sean
PS the stack overflow would explain what i want to say better.