Cannot collect coins

Hi you all, in my application, I used the following code to generate random coins, and it works properly:

local function coin()  
   
 local token = display.newImage("immagini/moneta.png")  
 token:setReferencePoint(display.CenterReferencePoint);  
 token.x = mRand(50, \_W-50);   
 --token.y = mRand(50, \_H-50)-100;  
 token.y = mRand(-100, -100)-100;  
 token.myName = "token"  
 physics.addBody(token,"static",{isSensor = true})  
  
 local function muovi()  
 token:translate(0, 1)  
 end  
   
 Runtime:addEventListener( "enterFrame", muovi );  
   
 end  
   
 tmr = timer.performWithDelay(500, coin, 0)  

then I added a collision event to let my player collect coins, but this one doesn’t work:

function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "token" and event.object2.myName == "player" then  
   
 token:removeSelf()  
 token = nil  
  
 end  
 end  

Any suggestion? Thanks in advantage! :slight_smile: [import]uid: 122056 topic_id: 35112 reply_id: 335112[/import]

The first problem is that inside onCollision() you don’t know which token is being removed. You’re token is local to your spawn function only, and even if it wasn’t it would reference the last one you created.

Secondly we can’t see where you’re creating your collision’s addEventListener() and are you certain that object1.myName is “token” and not “player” at that point? [import]uid: 199310 topic_id: 35112 reply_id: 139593[/import]

Are the newest tokens disappearing when you hit other tokens by any chance? You need to create a table for the tokens so that each token has it’s own unique ID number.

Try this:

token = {}  
i = 1  
local function coin()  
   
 local token[i] = display.newImage("immagini/moneta.png")  
 token[i]:setReferencePoint(display.CenterReferencePoint);  
 token[i].x = mRand(50, \_W-50);   
 --token.y = mRand(50, \_H-50)-100;  
 token[i].y = mRand(-100, -100)-100;  
 token[i].myName = "token"  
 physics.addBody( token[i],"static",{isSensor = true})  
   
 local function muovi()  
 token[i]:translate(0, 1)  
 end  
   
 Runtime:addEventListener( "enterFrame", muovi );  
 i = i + 1  
 end  
   
 tmr = timer.performWithDelay(500, coin, 0)  
   
function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "player" and event.object2.myName == "token" then  
 event.object2:removeSelf()  
 event.object2 = nil  
 elseif event.object1.myName == "token" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
 end  
 end  
end  

Not sure if event.object:removeSelf() works, if it doesn’t let me know and I can give you the solution to that. Sorry wrote this fast, let me know if any problems. [import]uid: 77199 topic_id: 35112 reply_id: 139592[/import]

Thank you both! I tried the code posted by hatethinkingofnames (Thanks!) but I got a white screen.

Anyway, here’s the complete code I’m using, and yes, object1.myName is token and object.myName is player.

[code]
local function coin()

local token = display.newImage(“immagini/moneta.png”)
token:setReferencePoint(display.CenterReferencePoint);
token.x = mRand(50, _W-50);
token.y = mRand(-100, -100)-100;
token.myName = “token”
physics.addBody(token,“static”,{isSensor = true})

local function muovi()
token:translate(0, 1)
end

Runtime:addEventListener( “enterFrame”, muovi );

end

tmr = timer.performWithDelay(500, coin, 0)

–COLLISIONS

function onCollision( event )
if ( event.phase == “began” ) then
if event.object1.myName == “lines” and event.object2.myName == “player” then

transition.to( event.object1, { alpha=0, time=200 } )

local function removeLines()
lines[i].parent:remove(lines[i])
lines[i] = nil
end

removetmrLines = timer.performWithDelay(200, removeLines, 1)

scrolling = true
score = score + 100

media.playSound( “suoni/boing1.wav” )

local function animate()
bg1:translate(0, 3)
bg2:translate(0, 3)
bg3:translate(0, 3)
bg4:translate(0, 3)
bg5:translate(0, 3)
bg6:translate(0, 3)
bg7:translate(0, 3)
bg8:translate(0, 3)
bg9:translate(0, 3)
bg10:translate(0, 3)
bg11:translate(0, 3)
bg12:translate(0, 3)
bg13:translate(0, 3)
bg14:translate(0, 3)
bg15:translate(0, 3)
bg16:translate(0, 3)
bg17:translate(0, 3)
bg18:translate(0, 3)
bg19:translate(0, 3)
bg20:translate(0, 3)
bg21:translate(0, 3)
end

Runtime:addEventListener( “enterFrame”, animate );

local function remove()
Runtime:removeEventListener(“enterFrame”, animate)
end

trmRemove = timer.performWithDelay(500, remove, 1)

end

elseif ( event.phase == “ended” ) then
if event.object1.myName == “token” and event.object2.myName == “player” then

–media.playSound( “suoni/boing1.wav” )

–event.object1:removeSelf()
–event.object1 = nil
–[[coin:removeSelf()
coin = nil
coins = coins + 1
coinsText.text = secCoins…coins
score = score + 10000

local fade = display.newImage(“immagini/fade.png”)
fade.alpha = 0

transition.to( fade, { alpha=1, time=200 } )

local function cambiaScena()
director:changeScene(“natale”)
storyboard.purgeScene( “game” )
end

timercambiaScena = timer.performWithDelay(201, cambiaScena, 1)

Runtime:removeEventListener(“enterFrame”, animate)]]–

end
end
end

Runtime:addEventListener( “collision”, onCollision )
[/code] [import]uid: 122056 topic_id: 35112 reply_id: 139598[/import]

 --event.object1:removeSelf()  
 --event.object1 = nil  

those lines should work. Why do you have them commented out?
[import]uid: 199310 topic_id: 35112 reply_id: 139603[/import]

Ahh yes sorry, thanks for your help :slight_smile:

I didn’t see that, now it works, with the only problem that coins are supposed to fall down from the top and the bottom of the screen, and they do that correctly but, when I collect the first coin, they stop moving, staying fixed on the screen. Do you know why?

Thanks again :slight_smile: [import]uid: 122056 topic_id: 35112 reply_id: 139665[/import]

The first problem is that inside onCollision() you don’t know which token is being removed. You’re token is local to your spawn function only, and even if it wasn’t it would reference the last one you created.

Secondly we can’t see where you’re creating your collision’s addEventListener() and are you certain that object1.myName is “token” and not “player” at that point? [import]uid: 199310 topic_id: 35112 reply_id: 139593[/import]

Are the newest tokens disappearing when you hit other tokens by any chance? You need to create a table for the tokens so that each token has it’s own unique ID number.

Try this:

token = {}  
i = 1  
local function coin()  
   
 local token[i] = display.newImage("immagini/moneta.png")  
 token[i]:setReferencePoint(display.CenterReferencePoint);  
 token[i].x = mRand(50, \_W-50);   
 --token.y = mRand(50, \_H-50)-100;  
 token[i].y = mRand(-100, -100)-100;  
 token[i].myName = "token"  
 physics.addBody( token[i],"static",{isSensor = true})  
   
 local function muovi()  
 token[i]:translate(0, 1)  
 end  
   
 Runtime:addEventListener( "enterFrame", muovi );  
 i = i + 1  
 end  
   
 tmr = timer.performWithDelay(500, coin, 0)  
   
function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "player" and event.object2.myName == "token" then  
 event.object2:removeSelf()  
 event.object2 = nil  
 elseif event.object1.myName == "token" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
 end  
 end  
end  

Not sure if event.object:removeSelf() works, if it doesn’t let me know and I can give you the solution to that. Sorry wrote this fast, let me know if any problems. [import]uid: 77199 topic_id: 35112 reply_id: 139592[/import]

Thank you both! I tried the code posted by hatethinkingofnames (Thanks!) but I got a white screen.

Anyway, here’s the complete code I’m using, and yes, object1.myName is token and object.myName is player.

[code]
local function coin()

local token = display.newImage(“immagini/moneta.png”)
token:setReferencePoint(display.CenterReferencePoint);
token.x = mRand(50, _W-50);
token.y = mRand(-100, -100)-100;
token.myName = “token”
physics.addBody(token,“static”,{isSensor = true})

local function muovi()
token:translate(0, 1)
end

Runtime:addEventListener( “enterFrame”, muovi );

end

tmr = timer.performWithDelay(500, coin, 0)

–COLLISIONS

function onCollision( event )
if ( event.phase == “began” ) then
if event.object1.myName == “lines” and event.object2.myName == “player” then

transition.to( event.object1, { alpha=0, time=200 } )

local function removeLines()
lines[i].parent:remove(lines[i])
lines[i] = nil
end

removetmrLines = timer.performWithDelay(200, removeLines, 1)

scrolling = true
score = score + 100

media.playSound( “suoni/boing1.wav” )

local function animate()
bg1:translate(0, 3)
bg2:translate(0, 3)
bg3:translate(0, 3)
bg4:translate(0, 3)
bg5:translate(0, 3)
bg6:translate(0, 3)
bg7:translate(0, 3)
bg8:translate(0, 3)
bg9:translate(0, 3)
bg10:translate(0, 3)
bg11:translate(0, 3)
bg12:translate(0, 3)
bg13:translate(0, 3)
bg14:translate(0, 3)
bg15:translate(0, 3)
bg16:translate(0, 3)
bg17:translate(0, 3)
bg18:translate(0, 3)
bg19:translate(0, 3)
bg20:translate(0, 3)
bg21:translate(0, 3)
end

Runtime:addEventListener( “enterFrame”, animate );

local function remove()
Runtime:removeEventListener(“enterFrame”, animate)
end

trmRemove = timer.performWithDelay(500, remove, 1)

end

elseif ( event.phase == “ended” ) then
if event.object1.myName == “token” and event.object2.myName == “player” then

–media.playSound( “suoni/boing1.wav” )

–event.object1:removeSelf()
–event.object1 = nil
–[[coin:removeSelf()
coin = nil
coins = coins + 1
coinsText.text = secCoins…coins
score = score + 10000

local fade = display.newImage(“immagini/fade.png”)
fade.alpha = 0

transition.to( fade, { alpha=1, time=200 } )

local function cambiaScena()
director:changeScene(“natale”)
storyboard.purgeScene( “game” )
end

timercambiaScena = timer.performWithDelay(201, cambiaScena, 1)

Runtime:removeEventListener(“enterFrame”, animate)]]–

end
end
end

Runtime:addEventListener( “collision”, onCollision )
[/code] [import]uid: 122056 topic_id: 35112 reply_id: 139598[/import]

 --event.object1:removeSelf()  
 --event.object1 = nil  

those lines should work. Why do you have them commented out?
[import]uid: 199310 topic_id: 35112 reply_id: 139603[/import]

Ahh yes sorry, thanks for your help :slight_smile:

I didn’t see that, now it works, with the only problem that coins are supposed to fall down from the top and the bottom of the screen, and they do that correctly but, when I collect the first coin, they stop moving, staying fixed on the screen. Do you know why?

Thanks again :slight_smile: [import]uid: 122056 topic_id: 35112 reply_id: 139665[/import]