如何在 Corona 的触摸函数中进行连续动作?

在我的游戏中,我使用applyLinearImpulse为倒立的男人提供力量。当我点击左右按钮改变x和y方向时,如何增加每个触摸的不同力量?

原文链接 https://stackoverflow.com/questions/9662631

点赞
stackoverflow用户909233
stackoverflow用户909233

以下是可以做的事情。

将开始移动事件的初始x、y保存为ixiy

对于每个移动事件,

计算ixevent.x之间的差异,并应用这个差异dx

对于y轴做相同的处理。

如果触摸事件结束,将初始x、yixiy设为空。

local function left:touch(event)
    if event.phase == "began" then
    --保存男孩的初始位置
        boy.ix,boy.iy = event.x,event.y
    elseif event.phase == "moved" then
        if boy.ix and boy.iy then
        --用当前事件的x、y差异计算初始x、y
            local dx = (event.x-boy.ix)*0.4
            local dy = (event.y-boy.iy)*0.4
            boy:applyLinearImpulse(dx,dy,boy.x,boy.y)
            --boy:applyForce(dx,dy,boy.x,boy.y)
            boy.ix,boy.iy = boy.x,boy.y
        end
    elseif event.phase == "ended" or event.phase == "cancelled" then
        boy.ix,boy.iy = nil,nil
    end
end
2012-03-12 10:36:24