【问题标题】:Stop a controlling object to move in Corona sdk停止控制对象在 Corona sdk 中移动
【发布时间】:2013-05-22 08:43:13
【问题描述】:

我在类似太空射击游戏的游戏中有 2 个控件,分别是左和右。 船确实左右移动,当船移动到 x=40 的左侧(最左边)时,它会恢复到 x=40 并暂时禁用“向左移动”功能,但我的问题是,每当我移动到屏幕的最左边或最右边,从屏幕中间,船移动得比 x=40(大约 x=35)更远,然后向后移动并继续,直到我松开按钮。 我怎样才能做到,当我将船移到最左边时,它会完全停止,只让我向右移动?

这是我的代码:

function left:touch()
if(car01.x>40 and car01.x<280) then
    motionx=-speed;
return false
end
end
left:addEventListener("touch",left)

function left:touch2()
    motionx=0;
end

local function movecar(event)
if(car01.x>40 and car01.x<280) then
    car01.x=car01.x+motionx
return false
elseif(car01.x==40) then
    car01.angularVelocity=0
    left:addEventListener("touch2",left)
    car01.x=car01.x+motionx
elseif(car01.x<40) then
    car01.x=40
end
end
Runtime:addEventListener("enterFrame",movecar)

local function stop(event)
if (event.phase=="ended") then
    motionx=0;
    return true
    end
end
Runtime:addEventListener("touch",stop)

【问题讨论】:

    标签: coronasdk


    【解决方案1】:

    这就是你要找的..?

    创建背景(bg),左右控件(左右分别)。那么:

    local function moveLeft()
      if(car01.x>40)then
        car01.x = car01.x - 40
      end
    end
    left:addEventListener("tap",moveLeft)
    
    local function moveRight()
      if(car01.x<280)then
        car01.x = car01.x + 40
      end
    end
    right:addEventListener("tap",moveRight)
    

    Keo 编码....... :)

    【讨论】:

    • 感谢您的回复!但我的问题是我的 car01 对象有点移动到屏幕外。顺便说一下,我的意思是即使我使用 display.newImage 和physics.addBody 设置了墙壁,当我通过按住我在屏幕上创建的左按钮将car01 向左移动时,car01 移动直到对象的左半部分是离开屏幕,然后整个对象移动到左墙的右侧,然后离开屏幕,然后移动到左墙的右侧,这种情况一直持续到我松开按钮为止。
    • 你的car01也是物理体吗?那么它可能是由于它与墙壁碰撞的问题。尝试增加左边框值(到 50 或 60)。然后再试一次。问题依然存在,那就用:car01.isSensor=true
    • 再次感谢您的回复!我尝试了 local left_wall=display.newRect(50,0,1,display.contentHeight) 然后 Physics.addBody(left_wall,"static") 以及 car01.isSensor=true,但他们似乎没有解决问题。我开始认为这是 Corona sdk 的限制。当car01达到x=40时,我通过设置motionx=0来解决这个问题,它遵循的方程是car01.x=car01.x+motionx。我不是 100% 满意,但比以前好多了。
    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多