在Corona SDK中检测触摸开始阶段的移动。

在我的应用程序中,我希望一个对象在 Corona 中的“touch began”事件上滑动和跳跃。虽然我成功地在“touch ended”事件上实现了滑动和跳跃,但是我不能在“began”事件上滑动。

我使用以下代码进行滑动和跳跃:

function touched( event )
  if(event.phase == "ended") then
    if(event.x - event.xStart > 70) then
      sliding = true;
      offGround = true;
    else
      boy:applyLinearImpulse(0, -0.44, boy.x, boy.y)
      offGround = true;
    end
  end
end
点赞
用户269870
用户269870

你无法做你想做的事情,因为事件的开始就是定义好的。因此,event.x 和 event.xStart 总是相同的,无法检测手指在事件开始时移动了多少,因为事件刚刚开始,因此没有过去可供比较。

2012-10-08 13:53:33
用户1514524
用户1514524

我已经做到了。我使用了移动阶段并在移动阶段上标记了一个布尔变量 active,并在开始时调用了一个计时器函数。以下是我的代码。

function Action()
    if(Slide == true)then
        Slide = false;
        sliding = true;
    else
        if(jump == true)then
            meter = -0.54
            boy:applyLinearImpulse(0, meter, boy.x, boy.y)
        end
    end
end

function touched( event )
    local phase = event.phase
    print(phase)

    if(phase == "moved")then
        Slide = true;
        jump = false;
        print("男孩正在滑动")
    end
    if(phase == "began")then
        if  Slide == false then
            print("男孩没有滑动")
            jump = true
        end

        timerNew = timer.performWithDelay(100 , Action , 1)
    end
end
2012-10-09 06:02:01