How to make a local collision event work?

Hi, I’m trying to create a game where when an object collides with the other type of object in the game a new body of the first object gets created.
I’ll paste the code so that hopefully you can understand better.

function onCollision(self,event)
if event.phase==“began” then
if == “cell” then
local newVirus = display.newImageRect(“virus.png”, 90,90)
physics.addBody( newVirus, { density=1, friction=0.5, bounce=0.5, radius=25 })

virus.collision = onCollision

Basically there is a virus and there are several cells. When a virus hits a cell a new virus with the same properties of the original one should be created.
This code is not working, what am I doing wrong?

What exactly isnt working?

From what I read, newVirus shold be created on collision but it will have position 0,0 and no velocity if gravity is off…
Or event isnt triggered at all? Did you try to put there few print statements to check if onCollision even executes?

It doesn’t work at all, I tried to put some print statements but it seems like the function isn’t even there… I don’t know what I’m doing wrong.

Well, you need to add your onCollision event when you add a newVirus

Hello Elena1,

I am not sure if this is a typo mistake when you did your post. Usually, an event name is of type “string” in quotes. So, based on your posted code, it should be,

virus.collision = onCollision
virus:addEventListener( "collision", virus )
-- or simply --
virus:addEventListener( "collision" )

Please check out this page for some details and examples.

And how can I do it? Do you have any examples I can look at?

I tried to change it but it didn’t work :pensive: I noticed that sometimes a new virus gets created after a collision but not always.

Hello Elena1,

I am no gamer app person, as I use Solar2D to build mostly business apps, tools and utilities. In any case, below are some code snippets what I have done a quick try,

function onCollision( self, event )
    local newVirus = display.newImageRect( “virus.png”, 90, 90 )
    newVirus.x = -- create new one at target position
    newVirus.y =
    timer.performWithDelay( 250, function() -- provide 250ms delay to add into physics
        physics.addBody( newVirus, "dynamic", { density=1, friction=0.5, bounce=0.5, radius=25 } ) -- specify as a dynamic
        newVirus:applyForce( 200, 200 ) -- move the new one away 
    end ) "collision" ) -- stop the old one = nil 
virus.collision = onCollision
virus:addEventListener( "collision" )

Of course, you would need to further enhance and customize this code. A detailed reading on the guide on physics as well as the tutorial on gaming / physics may help.
Guide on “physics”
Tutorial on “gaming / physics”

There is also a Solar2D Playground, where you could see a few examples of game with physics and collision, with codes provided. You could edit the codes and see your modified codes run in the browser.
Solar2D Playground, as provided by Eetu “XeduR” Rantanen

Or, there are others here on this forum who have more expertise and experience in gaming and physics, who may be to help you more.

1 Like

Thank you!