Hello everyone! Could someone help me with adding lives for the “enemy”? I want the enemy to lose 1 hp after a collision. I have many ideas but everyone doesn’t work.
This is code for enemy :
local function enemy2() local function miny2() local newMine2 = display.newImageRect("mina2.png", 35, 35) miny2HP = 3 physics.addBody(newMine2, "dynamic", {isSensor = true, radius=35}) newMine2.myName = "mina2" local whereFrom2 = math.random(5) if ( whereFrom2 == 1 ) then newMine2.x = 550 newMine2.y = 80 newMine2:setLinearVelocity(-150, 0) elseif ( whereFrom2 == 2 ) then newMine2.x = 550 newMine2.y = 148 newMine2:setLinearVelocity(-150, 0) elseif ( whereFrom2 == 3 ) then newMine2.x = 550 newMine2.y = 124 newMine2:setLinearVelocity(-150, 0) elseif ( whereFrom2 == 4 ) then newMine2.x = 550 newMine2.y = 176 newMine2:setLinearVelocity(-150, 0) elseif ( whereFrom2 == 5 ) then newMine2.x = 550 newMine2.y = 284 newMine2:setLinearVelocity(-150, 0) end end timer.performWithDelay( 1000, miny2, 20 ) end timer.performWithDelay( 10000, enemy2, 1 )
and this is my code for collision (bullet-enemy) :
local function onCollision2( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "bullet" and obj2.myName == "mina2" ) or ( obj1.myName == "mina2" and obj2.myName == "bullet" ) ) then display.remove( obj1 ) display.remove( obj2 ) end end end Runtime:addEventListener( "collision", onCollision2 )
I tried to separate obj1 and obj2 and add new functions, unfortunately my knowledge is too small, and I can’t find anywhere to explain for beginners how to do it.
If someone could take a moment to explain me and I would be very grateful
Sorry for the mistakes, my english is not perfect :unsure: