0

我不知道为什么我的代码不起作用。如果他面前的角色静止,我只想阻止下一个角色。我的想法是在他们的线速度为 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)
4

1 回答 1

1

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

  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
于 2021-03-16T13:57:04.603 回答