Call function only when colliding with a certain object

Hey guys how do I figure out when a object collides with a certain object then call the event lisatner. I am trying to make it so when the player collides with the enmey it will subtract health. But when it collides with the ground it subtracts health to ?

local function enemyattack( event )         if playerhlth\>1 then             playerhlth = playerhlth - 1             print("you are being attacked")                 print("your health is",playerhlth)             local urhealth = display.newText(playerhlth,square.x,square.y,25,25)             --score/health display urhealth:setFillColor(0,1,0)             transition.to(urhealth,{time= 4000,x=0,y=-500})         end              end     --square is main chareacter     square:addEventListener("collision",enemyattack)

Hi @tpjacobson01,

First thing is that your collision listener assignment seems incorrect. Change it to this:

[lua]

square.collision = enemyattack

square:addEventListener( “collision”, square )

[/lua]

Also, add the “self” parameter to your collision handling function:

[lua]

local function enemyattack( self, event )

[/lua]

Now, as for detecting only enemy collisions, you should assign all enemies a property, for example:

[lua]

local newEnemy = display.newImageRect( “enemy.png”, 32, 32 )

newEnemy.category = “enemy”

[/lua]

Then, in your collision handling function, conditionally check that property like this:

[lua]

local function enemyattack( self, event )

    if ( event.phase == “began” )  --CHECK ONLY WHEN COLLISION BEGINS

        --ADD THIS CONDITIONAL CHECK

        --remember that “event.other” references the object that the main character collided with (an enemy, the ground, etc.)

        if ( event.other.category == “enemy” ) then

            --subtract health, etc.

        end

    end

end

[/lua]

Take care,

Brent

Thank you so much Brent! It worked like a charm. :smiley: :smiley:

Hi @tpjacobson01,

First thing is that your collision listener assignment seems incorrect. Change it to this:

[lua]

square.collision = enemyattack

square:addEventListener( “collision”, square )

[/lua]

Also, add the “self” parameter to your collision handling function:

[lua]

local function enemyattack( self, event )

[/lua]

Now, as for detecting only enemy collisions, you should assign all enemies a property, for example:

[lua]

local newEnemy = display.newImageRect( “enemy.png”, 32, 32 )

newEnemy.category = “enemy”

[/lua]

Then, in your collision handling function, conditionally check that property like this:

[lua]

local function enemyattack( self, event )

    if ( event.phase == “began” )  --CHECK ONLY WHEN COLLISION BEGINS

        --ADD THIS CONDITIONAL CHECK

        --remember that “event.other” references the object that the main character collided with (an enemy, the ground, etc.)

        if ( event.other.category == “enemy” ) then

            --subtract health, etc.

        end

    end

end

[/lua]

Take care,

Brent

Thank you so much Brent! It worked like a charm. :smiley: :smiley: