I need to collect coins but at the same time dodge objects.

The problem I’m having is that the coins will randomly “glitch” around when contacting the thing I need to dodge.

I have 3 coins, one “player” and an object that randomly appears on the y axis and floats across the screen at various speeds. If I hit the random object, I die, but I want to be able to hit and collect the coins.

this is my code for one coin and the collision with movement function.

local yellowCoin = display.newCircle(_W*1.4, 0, 10)
yellowCoin:setFillColor(250,250,10)
physics.addBody(yellowCoin, “static”,{isSensor = true})
yellowCoin.myName = “coin1”
localGroup:insert(yellowCoin)

function yellowCoinsMove (event)
yellowCoin.x, yellowCoin.y = 0, math.random(50, 300)
yellowTran = transition.from(yellowCoin, {time=3000, x =_W*1.4, onComplete= yellowCoinsMove})
end

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

if event.object1.myName == “player” and event.object2.myName == “coin1” then
score.setScore (score.getScore()+10)

end

elseif ( event.phase == “ended” ) then

if event.object1.myName == “player” and event.object2.myName == “coin1” then

yellowCoinsMove()

end

end
end

yellowCoin:addEventListener(“collision”, yellowCoins)

[import]uid: 136878 topic_id: 24366 reply_id: 324366[/import]

You should try event.other.name rather than checking both objects in the collision - how do you know object1 will always be the player?

There’s a tutorial on collision detection in Corona For Newbies part 4 which may help you :slight_smile: [import]uid: 52491 topic_id: 24366 reply_id: 98434[/import]

The coins just keep going through the player without him picking them up with “object” or “other.” I don’t know what you mean by “how do you know object1 will always be the player?”

It is a side scroller game. I can best compare it to mario with the bullets that come at you. I have to dodge that thing while picking up coins along the way. I had it set up that on collision it would make the coin disappear and then start coming back again, but I get a debug error every time and the coins start jumping and randomly disappearing.

I’ve already looked through the collision detection, and I have tried, preCollision, postCollision, and collision. [import]uid: 136878 topic_id: 24366 reply_id: 98856[/import]

Did you look at the tutorial I suggested? It has a different method of collision detection that I believe you will find much, much easier to use and will fit the style of game you want to make.

Let me know :slight_smile: [import]uid: 52491 topic_id: 24366 reply_id: 98944[/import]