Corona SDK对象遵循“滑动”方向吗?

我正在尝试做一些非常简单的事情,因为我是 Corona SDK 的新手,但我遇到了很多问题。

我想要做的是,当用户在屏幕的某个区域(一个设置为屏幕一半的图像)"拖动","抛出","轻扫" 时,屏幕中央的一个球会以设定的速度向那个方向移动。没有物理或任何特殊的东西。所以基本上,如果您以某个角度轻扫图像,则球将向该角度移动,并最终离开屏幕,除非您再次以不同方向拖动球。

我目前拥有的是:

`` physics.addBody ( ball, "dynamic" )

function flick(event) if event.phase == "moved" then ball:applyForce( (ball.x) * 0.5, (ball.y) * 0.5, ball.x, ball.y ) end end ``

当前,当您移动球时,它只朝一个方向射出(0.5)如何获得 "抛出 "的方向?

我是 corona 的新手,仍在尝试弄清楚触摸事件。对我来说还有点困惑。

感谢大家的帮助和建议!谢谢!

点赞
用户1381216
用户1381216

根据外观来看, flick 是一个触摸监听器。您必须过滤传递的事件,并确定用户滑动的方向。了解更多关于 触摸监听器 的信息。

您感兴趣的是 xStartyStart 属性,以及测量用户移动的程度,类似于以下内容:

local function flick(event)
    if event.phase == "moved" then
        local diffX = event.xStart - event.x
        local diffY = event.yStart - event.y
        if math.abs(diffX) > 10 or math.abs(diffY) > 10 then
            ball:applyForce( (ball.x) * diffX, (ball.y) * diffY, ball.x, ball.y )
        end
    end
end
2014-06-12 19:15:35
用户869951
用户869951

你需要追踪触摸的起点和终点:这就是 flick。起点和终点在 x 和 y 方向上的差异将给出球的运动方向。如果球速度取决于用户 flick 的快慢,你也需要追踪时间。另外,我不确定为什么你要使力量与物体的位置成正比:这意味着如果物体位于 0,0,那么就没有力量,这可能不是你想要的。你只需要一个与 flick 向量成比例的力量。时间的长短将决定时间。它看起来像这样:

local flickStartTime = 0

local function touch(event)
    if event.phase == "began" then
        flickStartTime = system.timer()

    elseif event.phase == "ended" then
        local flickDuration = system.timer() - flickStartTime
        if flickDuration <= 0 then return end -- ignore flick

        local speedX = (event.xStart - event.x)/flickDuration
        local speedY = (event.yStart - event.y)/flickDuration
        local coef = 0.1 -- adjust this via "trial and error" so moves at "reasonable" speed
        -- only react to flick if it was large enough
        if math.abs(event.xStart - event.x) > 10 or math.abs(event.yStart - event.y) > 10 then
            ball:applyForce( coef * speedX, coef * speedY, ball.x, ball.y )
        end
    end
end

然而,你说你不想有任何物理效果,但你应用了力量。你是指你只想设置速度吗?那么,你需要调用 setLinearVelocity(speedX, speedY) 来替代 applyForce

local speedX = (event.xStart - event.x)/flickDuration
local speedY = (event.yStart - event.y)/flickDuration
local coef = 0.1 -- adjust this via "trial and error" so moves at "reasonable" speed
-- only react to flick if it was large enough
if math.abs(event.xStart - event.x) > 10 or math.abs(event.yStart - event.y) > 10 then
    ball:setLinearVelocity( coef * speedX, coef * speedY )
end
2014-06-13 02:08:18