【问题标题】:How to stop a character if the character in front of him is still如果他面前的角色还在,如何阻止角色
【发布时间】:2021-04-22 04:24:04
【问题描述】:

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

【问题讨论】:

    标签: coronasdk solar2d


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多