Problem with collision

Hello everyone, I have problem with my game. I’m trying to make a game where after touching the wall our live decreases by 1 and we appear at the beginning. We move with the mouse, where we press, where the object goes. Unfortunately, I can’t do that after the first touch of the wall, the move is stopped and we immadently appear at the beginning with live -1 , there is a possibility that someone will click on the end of the map, where along the way there will be several walls and will lose several lives.

This is the code for this part:

local function ruch(event) if event.phase == "began" then transition.to(statek, {time = 500, x=event.x, y=event.y}) end end Runtime:addEventListener("touch", ruch) local function restoreShip() statek.isBodyActive = false statek.x = display.contentCenterX statek.y = display.contentCenterY transition.to( statek, { alpha=1, time=1000, onComplete = function() statek.isBodyActive = true died = false end }) end local function onCollision(event) if event.phase == "began" then local obj1 = event.object1 local obj2 = event.object2 if (( obj1.myName == "statek" and obj2.myName == "labirynt") or ( obj1.myName == "labirynt" and obj2.myName == "statek")) then local ship local wall if obj1.myName == "statek" then ship = obj1 wall = obj2 elseif obj2.myName == "statek" then ship = obj2 wall = obj1 end if ship then died = true statek.alpha = 0 timer.performWithDelay( 501, restoreShip ) if died == true then Lives = Lives - 1 LivesText.text = "Lives= " .. Lives end end end end end Runtime:addEventListener("collision", onCollision)

Thank you all for every advice :slight_smile:

Hi @Ceterge,

Try

local function onCollision(event)   if event.phase == "began" then     local obj1 = event.object1     local obj2 = event.object2     if (( obj1.myName == "statek" and obj2.myName == "labirynt") or        ( obj1.myName == "labirynt" and obj2.myName == "statek")) then -- Do you need ship and wall variables?        local ship        local wall              if obj1.myName == "statek" then          ship = obj1          wall = obj2       elseif obj2.myName == "statek" then         ship = obj2         wall = obj1       end       -- I added additional condition       if ship and not died then         died = true         statek.alpha = 0         timer.performWithDelay( 501, restoreShip )         -- I removed if statement since it is unnecessary         Lives = Lives - 1         LivesText.text = "Lives= " .. Lives       end     end   end end

Hava nice day

ldurniat

almost everything is fine, the only problem is with :

 transition.to( statek, { alpha=1, time=2000, onComplete = function() statek.isBodyActive = true died = false

when it appears, you can move around the map without working function that takes live and sees collisions.He is like a “ghost” then

To stop ship after death use code below

local function onCollision(event) ...   if ship and not died then     died = true     statek.alpha = 0     transition.cancel( ship )  -- new code     timer.performWithDelay( 501, restoreShip )     -- I removed if statement since it is unnecessary     Lives = Lives - 1     LivesText.text = "Lives= " .. Lives   end ... end

Does ship is partially transparent?

Don’t use isBodyActive at all. Try

local function restoreShip()   died = false   --statek.isBodyActive = false   statek.x = display.contentCenterX   statek.y = display.contentCenterY   transition.to( statek, { alpha=1, time=1000,           --onComplete = function()           --   statek.isBodyActive = true           --    died = false          --end   }) end