I have a game where a player collects coins. When the player and the coin collide, the coin is supposed to go to the top left of the screen. The problem is that I am using the same variable for all coins, and Im using a function to make them constantly appear. When the player collides with the coin, all coins are moving to the top left of the screen, not just the one that hit the player.
Here is my code:
[lua]
–COINS
function updateRandomCoin(event)
check3 = math.random(100)
if(check3 > 50) then
coin = display.newImage(“coin1.png”, 1200, 550 - check3, true)
coin.xScale = 0.6
coin.yScale = 0.6
coin.type = “coin”
physics.addBody(coin, “kinematic”, {radius = 30, isSensor = true})
transition.to( coin, { time = (5000), x= -200, rotation = 1000} )
end
end
timer.performWithDelay(1200, updateRandomCoin, -1)
–COLLISION DETECTION
local function onCollision(event)
if event.phase == “began” then
local agro = event.object1
local hit = event.object2
if agro.type == “player” and hit.type == “coin” then
transition.to( coin, { time = (300), x= 225, y = 0, rotation = 1000} )
end
end
end
Runtime:addEventListener(“collision”, onCollision)
[/lua]
Thanks in advance