0

I don't know why my code doesn't work. I just want to stop the next character if the character in front of him is still. My thought was to setLinearVelocity(0,0) when their linear velocity is 0 so the next character know that he has to stop when the linear velocity of the character in front of him is 0.

local function loopPg()

 local runningPG = display.newSprite(pg[math.random(5)], sequences_runningPG)
 runningPG.x = display.contentCenterX
 runningPG.y = display.contentCenterY-730
 runningPG:scale(0.75, 0.75)
 runningPG:play()
 physics.addBody(runningPG, "dynamic", {radius = 55})


 local function pathPg()
    if(runningPG.y >= -190 and runningPG.y < 160) then
        runningPG:setLinearVelocity(0,250)
    elseif (runningPG.y >= 160 and runningPG.x >= 220) then
        runningPG:setLinearVelocity(-250,0)
    elseif (runningPG.x <= 220 and runningPG.y <= 635) then
        runningPG:setLinearVelocity(0,250)
    elseif ( runningPG.y >= 635) then
        runningPG:setLinearVelocity(0,0)
    end
 end

   local vx,vy = runningPG:getLinearVelocity()
   if(vx == 0 and vy == 0) then
     runningPG:setLinearVelocity(0,0)
   end

   Runtime:addEventListener( "enterFrame", pathPg )
end

timer.performWithDelay(600, loopPg, 3)
Jello
  • 11
  • 1

1 Answers1

1

Here is something that could help you:

  1. characters velocity function has to be involved in the enterFrame
  2. then you have to take the character's velocity from the character in front

This is not a solution, but it could help you

    local function pathPg()
        local vx,vy = 0,0 -- this has to be the velocity of the character in front 
        if(runningPG.y >= -190 and runningPG.y < 160) then
            vx,vy = 0,250
            runningPG:setLinearVelocity(vx,vy)
        elseif (runningPG.y >= 160 and runningPG.x >= 220) then
            vx,vy = -250, 0
            runningPG:setLinearVelocity(vx,vy)
        elseif (runningPG.x <= 220 and runningPG.y <= 635) then
            vx,vy = 0,250
            runningPG:setLinearVelocity(vx,vy)
        elseif ( runningPG.y >= 635) then
            vx,vy = 0,0
            runningPG:setLinearVelocity(vx,vy)
        end
     end
    
       Runtime:addEventListener( "enterFrame", pathPg )
    end
vpoltave
  • 1,612
  • 3
  • 14
  • 31