如果角色前面的角色是静止的,如何让角色停下来



我不知道为什么我的代码不工作。如果前面的角色还在,我只想让下一个角色停下来。我的想法是setLinearVelocity(0,0),当他们的线速度为0,所以下一个角色知道他必须停止,当他前面的角色的线速度为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)

这里有一些东西可以帮助你:

  1. 字符速度函数必须涉及到enterFrame
  2. 那么你必须从前面的角色那里获取角色的速度

这不是一个解决方案,但它可以帮助你

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

最新更新