Question About "if" and "elseIf"

I don’t know why, but the code after “elseif” doesn’t work.
The first part of “if” function works, but second doesn’t work, it doesn’t print anything even if the beam is coliding with zombie…

local function zombieCollision(self, event)
   if event.other == hero then

      if event.phase == "began" then
         hero.isColliding = true
         checkHeroCollision()
      elseif event.phase == "ended" then
         hero.isColliding = false
      end
      
   elseif event.other == beam then
      print("ouch")

   end
end

(Post edited by moderator; Please format code in posts; Click pencil icon to review changes.)

Can you share a sample project where we can see how you define hero and beam? (It would be a good idea to use simple rectangles / circles as placeholder images)

  1. I am assuming you did this:
local zombie = someCreateFunction()
zombie.collision = zombieCollision
zombie:addEventListenter( 'collision' )
  1. If you delete the zombie before the collision ends, you won’t get an ‘ended’ phase.

ok, thanks, but I am asking about this part:

...
   elseif event.other == beam then
      print("ouch")

   end
end

Yes, sure.
I will do it later today or tomorrow, because I haven’t much time today.

@ConveyedRex7592

Try making this change to your code so you can debug it on your own more easily:

local function zombieCollision(self, event)
   print( "zombieCollision event.other == ", event.other, event.time )
   print( "zombieCollision hero == ", hero, event.time )
   print( "zombieCollision beam == ", beam, event.time )
   print( "zombieCollision hero.isColliding == ", hero.isColliding, event.time )
   print( "zombieCollision other =? hero ", event.other == hero, event.time )
   print( "zombieCollision other =? beam ", event.other == beam, event.time )
   print ( "----------------\n")
   if event.other == hero then

      if event.phase == "began" then
         hero.isColliding = true
         checkHeroCollision()
      elseif event.phase == "ended" then
         hero.isColliding = false
      end
      
   elseif event.other == beam then
      print("ouch")

   end
end

I did this and when beam collides with zombie it says hero false and beam false

The thing is in sample everythings works perfectly… :thinking:

Sample.zip (51.6 KB)

I looked at your example. The ‘zombie’ never collides with the ‘beam’, so that statement will never trigger.

I tweaked your sample, look in the folder named ‘tweaked’. It now hits both and all *if sections trigger.
tweaked_sample.zip (107.6 KB)