Make specific objects do something, not all with the same name

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

Aren’t you storing your coin in a table?
 

coins = {}

What you want to do will be done if all your coins is in a table =)

Hope this helps

how would i implement that table? sorry, kind of new to this

Oh, I will just change some parts of your updateSomeCoin function to do that. This is how it looks

 

"Add this in your declaration" local coins = {} function updateRandomCoin(event) "please local check3 and coin to prevent memory leak =)" local check3 = math.random(100) if(check3 \> 50) then local 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}) "add this line" table.insert(coins, coin) transition.to( coin, { time = (5000), x= -200, rotation = 1000} ) "and that should do it =)" end end

and now you will access you coin in this way:
 

coins[index]

note that to properly remove the a child inside coins table you need to do it like this *just refer to this post, I just posted here* =)

http://forums.coronalabs.com/topic/33900-whats-the-best-way-to-create-objects-in-a-table-and-delete-them/#entry175649

Hope this helps =)

Thanks! I really appreciate all of this, although I still cant get just the specific coin that has collided to move. In my collision script I tried putting in “coins[index]” instead of just “coin” and none of the coins move. Is there anything else I have to do?

If you are doing it inside onCollision(event) you can do the moving this way as an alternative:

 

local function onCollision(event) local agro = event.object1 local hit = event.object2 if agro.type == "player" and hit.type == "coin" then "this way" transition.to(hit, {params}) "other way" transition.to(coints[hit.index], {params}) end end

By doing the other way you should refer to that link I provided =D because you will add index to the coins table.

Hope that helps =)

It works, thanks soooo much!

Aren’t you storing your coin in a table?
 

coins = {}

What you want to do will be done if all your coins is in a table =)

Hope this helps

how would i implement that table? sorry, kind of new to this

Oh, I will just change some parts of your updateSomeCoin function to do that. This is how it looks

 

"Add this in your declaration" local coins = {} function updateRandomCoin(event) "please local check3 and coin to prevent memory leak =)" local check3 = math.random(100) if(check3 \> 50) then local 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}) "add this line" table.insert(coins, coin) transition.to( coin, { time = (5000), x= -200, rotation = 1000} ) "and that should do it =)" end end

and now you will access you coin in this way:
 

coins[index]

note that to properly remove the a child inside coins table you need to do it like this *just refer to this post, I just posted here* =)

http://forums.coronalabs.com/topic/33900-whats-the-best-way-to-create-objects-in-a-table-and-delete-them/#entry175649

Hope this helps =)

Thanks! I really appreciate all of this, although I still cant get just the specific coin that has collided to move. In my collision script I tried putting in “coins[index]” instead of just “coin” and none of the coins move. Is there anything else I have to do?

If you are doing it inside onCollision(event) you can do the moving this way as an alternative:

 

local function onCollision(event) local agro = event.object1 local hit = event.object2 if agro.type == "player" and hit.type == "coin" then "this way" transition.to(hit, {params}) "other way" transition.to(coints[hit.index], {params}) end end

By doing the other way you should refer to that link I provided =D because you will add index to the coins table.

Hope that helps =)

It works, thanks soooo much!