help with collisions and translations

Hi guys, as you can see from the title, I’m having trouble with collisions and translations…

here’s the code I’m using, followed by my problems:

local function coin()  
   
 local coin = display.newCircle(5, 5, 5)  
 coin:setReferencePoint(display.CenterReferencePoint);  
 coin.x = mRand(50, \_W-50);   
 coin.y = mRand(50, \_H-50);  
 coin.myName = "coin"  
   
 end  
   
 tmr = timer.performWithDelay(20, coin, max)  
   
 local function animate()  
 coin:translate(0, 1)  
 Runtime:addEventListener( "enterFrame", animate );  
 end  
   
  
   
   
   
  
 physics.addBody(coin,"static",{isSensor = true})  
   
 function onCollision( event )  
 if ( event.phase == "began" ) then  
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
   
 event.object1.alpha = 0.001  
  
 end  
   
 elseif ( event.phase == "ended" ) then  
   
 if event.object1.myName == "coin" and event.object2.myName == "player" then  
 event.object1:removeSelf()  
 event.object1 = nil  
  
  
 end  
   
 end  
 end  
  
 Runtime:addEventListener( "collision", onCollision )  

If “coin” collides with “player”, it dissapears, but if I use a function to spawns more than one coin (in my case 20), they won’t be collected by “player”… in other words, they don’t dissapear and I don’t know why, any suggestions?

And then I’ve got the same problem with translations, so if the coin is only one, it will translate, but if I spawn more than a coin, they stay fixed to their original position.

I’d really appreciate your help

Thanks :slight_smile:

[import]uid: 122056 topic_id: 34017 reply_id: 334017[/import]

I think the problem here is you are using a function called “coin” but you’re also trying to access coin like it’s an object. [import]uid: 199310 topic_id: 34017 reply_id: 135288[/import]

Thanks for your help, but it still doesn’t work :frowning:

I used the following code:

[code]
local function coin()

local token = display.newCircle(5, 5, 5)
token:setReferencePoint(display.CenterReferencePoint);
token.x = mRand(50, _W-50);
token.y = mRand(50, _H-50);
token.myName = “token”

end

tmr = timer.performWithDelay(20, coin, max)

local function animate()
token:translate(0, 1)
Runtime:addEventListener( “enterFrame”, animate );
end

physics.addBody(token,“static”,{isSensor = true})

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

event.object1.alpha = 0.001

end

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

if event.object1.myName == “token” and event.object2.myName == “player” then
event.object1:removeSelf()
event.object1 = nil

end

end
end

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

You also have a scope problem.

On line 3, you create a token, but it is scoped to be local to that function only. You never return the token back to the caller (and in this case, you shouldn’t since the timer won’t know what to do with it), so in effect, while that circle is created and put on the screen you are loosing all references to that object.

On lines 14 and 23, you are accessing some variable named token that is NOT the same as the one you create in line 3. I don’t know what that is since I doubt this is all of your code. You are adding your physics body to something that is not the token you are creating in line 3 nor are you translating it. Try moving the addBody bit inside your spawn function and perhaps your translate as well. [import]uid: 199310 topic_id: 34017 reply_id: 135320[/import]

I think the problem here is you are using a function called “coin” but you’re also trying to access coin like it’s an object. [import]uid: 199310 topic_id: 34017 reply_id: 135288[/import]

Thanks for your help, but it still doesn’t work :frowning:

I used the following code:

[code]
local function coin()

local token = display.newCircle(5, 5, 5)
token:setReferencePoint(display.CenterReferencePoint);
token.x = mRand(50, _W-50);
token.y = mRand(50, _H-50);
token.myName = “token”

end

tmr = timer.performWithDelay(20, coin, max)

local function animate()
token:translate(0, 1)
Runtime:addEventListener( “enterFrame”, animate );
end

physics.addBody(token,“static”,{isSensor = true})

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

event.object1.alpha = 0.001

end

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

if event.object1.myName == “token” and event.object2.myName == “player” then
event.object1:removeSelf()
event.object1 = nil

end

end
end

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

You also have a scope problem.

On line 3, you create a token, but it is scoped to be local to that function only. You never return the token back to the caller (and in this case, you shouldn’t since the timer won’t know what to do with it), so in effect, while that circle is created and put on the screen you are loosing all references to that object.

On lines 14 and 23, you are accessing some variable named token that is NOT the same as the one you create in line 3. I don’t know what that is since I doubt this is all of your code. You are adding your physics body to something that is not the token you are creating in line 3 nor are you translating it. Try moving the addBody bit inside your spawn function and perhaps your translate as well. [import]uid: 199310 topic_id: 34017 reply_id: 135320[/import]