Collision is causing other objects transitions to stop.

Hi,

I have managed to get a bullet to destroy a ship and remove both objects. However, when the collision occurs it appears to cancel the transitions of other objects in the scene.

I have included the code for spawning a hoop, which grow before destroying itself, and, firing a bullet and destroying itself with the ship it collides with. But why would it effect the transitions of objects in other functions?

[blockcode] function spawnHoop(event)

if dospawn == 1 then
if speedlevel == 1 then
hoop = display.newImage(“circlegreen.png”)
gamescreen:insert( 1 , hoop )
dospawn = 0
hoop.alpha = 0.2
hoop.xScale = 0.01
hoop.yScale = 0.01
hoop.x = centreX
hoop.y = centreY
transition.to( hoop, { time=1000, alpha = 1,} )
transition.to( hoop, { time=1500, xScale = 1, yScale = 1, onComplete = destroyHoop } )
timer.performWithDelay(500, resetSpawn, 1 )
end
end
end[/blockcode]

[blockcode]function fire()
bullet = display.newCircle( ship.x, ship.y, 3 )
physics.addBody( bullet, “static”, redBody )
gamescreen:insert( 2, bullet )
bullet.collision = onLocalCollision
laserchannel = audio.play( laser )
transition.to(bullet, { time = 500, x = centreX, y = centreY, width = 0.25, height = 0.25, onComplete = destroyHoop } )
bullet:addEventListener( “collision”, bullet )
end
function onLocalCollision( self, event )
if ( event.phase == “began” ) then

event.target:removeSelf()
event.other:removeSelf()
bullet:removeEventListener(“collision”, bullet )

end
end[/blockcode]

Thanks
[import]uid: 8699 topic_id: 5451 reply_id: 305451[/import]

I had same problem

Try this

  
local hoop -- make sure this is on top  
local bullet -- make sure this is on top so compiler knows abount before you function   
function spawnHoop(event)  
 if dospawn == 1 then  
 if speedlevel == 1 then  
 hoop = display.newImage("circlegreen.png")  
 gamescreen:insert( 1 , hoop )  
 dospawn = 0  
 hoop.alpha = 0.2  
 hoop.xScale = 0.01  
 hoop.yScale = 0.01  
 hoop.x = centreX  
 hoop.y = centreY  
 transition.to( hoop, { time=1000, alpha = 1,} )  
 transition.to( hoop, { time=1500, xScale = 1, yScale = 1, onComplete = destroyHoop } )  
 timer.performWithDelay(500, resetSpawn, 1 )  
 end  
 end  
 end  
  
function fire()  
 bullet = display.newCircle( ship.x, ship.y, 3 )  
 physics.addBody( bullet, "static", redBody )  
 gamescreen:insert( 2, bullet )  
 bullet.collision = onLocalCollision  
 laserchannel = audio.play( laser )  
 transition.to(bullet, { time = 500, x = centreX, y = centreY, width = 0.25, height = 0.25, onComplete = destroyHoop } )  
 bullet:addEventListener( "collision", bullet )  
 end  
 function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
   
 event.target:removeSelf()  
 event.other:removeSelf()  
 bullet:removeEventListener("collision", bullet )  
  
 end  
 end  

I might be wrong but thats exactly what i had to do to fix my problem. [import]uid: 11559 topic_id: 5451 reply_id: 18385[/import]

Thanks for the reply. Was just coming to update this thread. I have worked out that its the onComplete command in my transition causing the problem.

if the bullet or ship is destroyed before the transition finishes, it still tries to complete the onComplete command after the given time, causing a massive freeze in the game.

I have the collision and destroy working now, but if a bullet doesn’t hit a ship, the ship and bullet stay on screen when they get to the end of the transition. Arrrrgh, i’ll suss it at some point! [import]uid: 8699 topic_id: 5451 reply_id: 18386[/import]

I cant see where you distroying it

try this

[code]

– After colission do whather and then destroy

bullet:removeSelf() [import]uid: 11559 topic_id: 5451 reply_id: 18387[/import]

its in this piece of code, this destroys the bullet.

[blockcode]
function onLocalCollision( self, event )
if ( event.phase == “began” ) then

event.target:removeSelf()
event.other:removeSelf()
bullet:removeEventListener(“collision”, bullet )

end
end

[/blockcode]

This isn’t the problem now though, its the transition complete line which was causing problems. I have solved it by getting rid of the line in the transition which destroys, and creating an invisible wall which destroys the object if it touches.

Thanks for the reply.
[import]uid: 8699 topic_id: 5451 reply_id: 18396[/import]

@gazjm
could you post your final solution?
I have similar problem: http://developer.anscamobile.com/forum/2011/01/23/cant-add-body-new-spawn-image

It started by trying to spawn new images on the fly and adding body to objects, but apparently that’s not allowed.

Now I have the issue of “nil” cuz the onComplete transition never completed :frowning:

I wished it could self determined that the object is no longer present and transition doesn’t have to “complete”

RD [import]uid: 7856 topic_id: 5451 reply_id: 18412[/import]

Sorry for the delay, different timezones! I have pasted the code I think is appropriate, and tried to make useful comments!

[blockcode]
function fire() ---- Function to fire a bullet -----

bullet = display.newCircle( ship.x, ship.y, 3 )
physics.addBody( bullet, redBody )
gamescreen:insert( 2, bullet )
bullet.collision = onCollision
laserchannel = audio.play( laser )
trans = transition.to(bullet, { time = 2000, x = centreX, y = centreY, width = 0.25, height = 0.25 } )
bullet:addEventListener( “collision”, bullet )
end

---- function to spawn an enemy ship -------
function spawnobst(event)

if dospawnE == 1 then

Enemylane = mRand(1,2)
enemyship = display.newImage(“shipafterburner.png”)
physics.addBody( enemyship, “static”, redBody )
gamescreen:insert( 1 , enemyship )
enemyship.objectName = “ship”
enemyship.alpha = 0.2
enemyship.xScale = 0.01
enemyship.yScale = 0.01
enemyship.x = centreX
enemyship.y = centreY
dospawnE = 0
if Enemylane == 1 then
enemyship.rotation = lane1rotation
transition.to( enemyship, { time=3000, xScale = 0.5, yScale = 0.5, x = lane1x, y = lane1y, transition=easing.inQuad } )
----- Spawns an object which destroys a bullet if the bullet hits it --------
bulletkill = display.newCircle( centreX, centreY, 25 )
physics.addBody( bulletkill, “static”, { radius = 25 } )
gamescreen:insert( 1, bulletkill )
bulletkill:setFillColor(255, 255, 0, 25 )
bulletkill.objectName = “bulletkill”
----- Collision function if bullet hits a ship, it removes itself and the ship, if it hits the bulletkill, then it removes itself(this removes the need for onComplete = bullet:removeSelf() at the end of the transition ------
function onCollision( self, event )
if ( event.phase == “began” ) then
if( event.other.objectName ==“ship” ) then
event.target:removeSelf()
event.other:removeSelf()
bullet:removeEventListener( “collision”, bullet )
end
if( event.other.objectName == “bulletkill” ) then
event.target:removeSelf()
bullet:removeEventListener( “collision”, bullet )
end
end
end [/blockcode] [import]uid: 8699 topic_id: 5451 reply_id: 18532[/import]