Need collision routine help PLEASE

Ok,
I am now at the stage of pulling my hair out.
I have been plodding slowly and resolving issues as I go along but seem to have hit a wall.

I will try my best to describe the help I need.
I have an animated bird on screen. (main player)
I have a leaf which transitions across the screen and strikes the main player object.
I have set up a simple collision detection here:

local bird = display.newSprite( sheet, { name="bird", start=1, count=9, time=1000 } )  
bird.x = display.contentWidth / 4 + 40  
bird.y = baseline - 120 --set height from baseline  
bird.xScale = 1  
bird.yScale = 1  
bird:play()  
  
physics.addBody(bird, "dynamic")  
physics.setGravity(0,0)  
  
bird.myName = "bird"  
ground.myName = "ground"  
bird:addEventListener("collision", bird)  
  
 function bird:collision (event)  
 if event.other.myName == "ground" then  
 print "bird hit the ground!"  
 end  
end  

and this for the object:

 --add leaf  
local leaf = display.newImage( "images/leafs.png" )  
physics.addBody(leaf, "dynamic")  
local w,h = display.viewableContentWidth, display.viewableContentWidth  
leaf.myName = "leaf"  
  
leaf:addEventListener("collision", leaf)  
  
function leaf:collision (event)  
 if event.other.myName == "bird" then  
 print "leaf hit the bird!"  
 end  
end  
  
transition.from( leaf, { time=4000, x=(w+400), y=(h-300), onComplete=function () leaf:removeSelf()end ;})  

all this works nicely, I have also implemented an analogue joystick for the bird, and parallax scrolling the the background. The collision events are working in the terminal.

Question is can someone tell me how to on collision the leaf will destroy itself and replace itself with a new sprite (exploding stars).
It must be pretty simple and any help will be much appreciated, I would even consider a payment to someone so I can move forward with this.

Thanks
[import]uid: 127675 topic_id: 30147 reply_id: 330147[/import]

I would create a function called:

spawnStars(x,y)

that creates your stars, perhaps pass the x, y coordinates of where you want the stars to appear. Then create a 2nd function that will remove your leaf:

local function removeLeaf(target)  
 local x = target.x  
 local y = target.y  
 target:removeSelf()  
 target = nil  
 spawnStars(x,y)  
end  

Where you have this line:

print “leaf hit the bird!”

add this line:

timer.performWithDelay(10, function() removeLeaf(event.target) end)  

The reason for this timer is that you can’t remove the physics object while the collision function is still running. 10 milliseconds should be plenty for that collision function to finish. The function() and end around the call to removeLeaf lets you pass the leaf in question to the function. In Lua terms this called a “closure”, in most other languages, it’s known as an anonymous function.

[import]uid: 19626 topic_id: 30147 reply_id: 120683[/import]

@rob,

thank-you for replying but I am still struggling a little.
Having looked at the ansca standard “balloon pop” example this does what I require once the leaf hits the bird;
i.e. has moving object that hits something then serves a sprite sheet object and then vanishes.
I think I,am having issues understanding the code and using it in my code.(perhaps my code is complementing matters?, maybe a better to write it?)
Ideally I would love a working example integrating my code, is this possible that some kind developer here would help me?
I feel if I can see an example which directly relates to my code above I will finally understand it.
Thing is Ive come a long way now and wished I had gone down the OOP route but I will learn on my next project.
This collision and returning another object happens all the time 90% of games, I see it as a real useful piece of code.

I am willing to pay for a well commented example if nessacary seeing as I havnt really contributed to the forum as yet.

God bless you all [import]uid: 127675 topic_id: 30147 reply_id: 120904[/import]

Hi,
just an update after making some progress or what seems like it.

I now have the collision events trigering an explosion off a sprite sheet. hooray!
But,
The leaf is removing itself but the sprite sheets last frame of the explosion still remains…
I have tried adding code to remove but it isn’t doing anything.
The code is compiling clean too.
Basically I need to remove the last frame of the sprite sheet?

Anybody?

thank you [import]uid: 127675 topic_id: 30147 reply_id: 121029[/import]

I would create a function called:

spawnStars(x,y)

that creates your stars, perhaps pass the x, y coordinates of where you want the stars to appear. Then create a 2nd function that will remove your leaf:

local function removeLeaf(target)  
 local x = target.x  
 local y = target.y  
 target:removeSelf()  
 target = nil  
 spawnStars(x,y)  
end  

Where you have this line:

print “leaf hit the bird!”

add this line:

timer.performWithDelay(10, function() removeLeaf(event.target) end)  

The reason for this timer is that you can’t remove the physics object while the collision function is still running. 10 milliseconds should be plenty for that collision function to finish. The function() and end around the call to removeLeaf lets you pass the leaf in question to the function. In Lua terms this called a “closure”, in most other languages, it’s known as an anonymous function.

[import]uid: 19626 topic_id: 30147 reply_id: 120683[/import]

@rob,

thank-you for replying but I am still struggling a little.
Having looked at the ansca standard “balloon pop” example this does what I require once the leaf hits the bird;
i.e. has moving object that hits something then serves a sprite sheet object and then vanishes.
I think I,am having issues understanding the code and using it in my code.(perhaps my code is complementing matters?, maybe a better to write it?)
Ideally I would love a working example integrating my code, is this possible that some kind developer here would help me?
I feel if I can see an example which directly relates to my code above I will finally understand it.
Thing is Ive come a long way now and wished I had gone down the OOP route but I will learn on my next project.
This collision and returning another object happens all the time 90% of games, I see it as a real useful piece of code.

I am willing to pay for a well commented example if nessacary seeing as I havnt really contributed to the forum as yet.

God bless you all [import]uid: 127675 topic_id: 30147 reply_id: 120904[/import]

Hi,
just an update after making some progress or what seems like it.

I now have the collision events trigering an explosion off a sprite sheet. hooray!
But,
The leaf is removing itself but the sprite sheets last frame of the explosion still remains…
I have tried adding code to remove but it isn’t doing anything.
The code is compiling clean too.
Basically I need to remove the last frame of the sprite sheet?

Anybody?

thank you [import]uid: 127675 topic_id: 30147 reply_id: 121029[/import]