Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua Corona SDK对象的后面是;“刷卡”;方向?_Lua_Coronasdk - Fatal编程技术网

Lua Corona SDK对象的后面是;“刷卡”;方向?

Lua Corona SDK对象的后面是;“刷卡”;方向?,lua,coronasdk,Lua,Coronasdk,我尝试做一些非常简单的事情,因为我是Corona SDK的新手,但是我有很多问题 我试图做到的是,当用户在屏幕的某个区域“擦拭”、“甩动”、“闪烁”(画面设置为屏幕的一半)时,屏幕中间的一个球以一个设定的速度向那个方向移动。没有物理或任何特别的东西。因此,基本上,如果你以某个角度滑动图像,球将移动到该角度,并最终离开屏幕,除非你以不同的方向再次“滑动”球 到目前为止,我得到的是: physics.addBody( ball, "dynamic" ) function flick(event)

我尝试做一些非常简单的事情,因为我是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)射出,我如何才能得到“抛”的方向

我是科罗纳的新手,仍在努力找出触碰事件。我还是有点困惑


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

从外观上看,
flick
是一个触摸式听众。您必须过滤传递的事件,并确定用户刷卡的方向。阅读更多关于

您感兴趣的是
xStart
yStart
属性,并测量用户移动了多少,如下所示:

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

您需要跟踪触摸的开始和结束位置:这是flick。沿x和y方向开始和结束之间的差值将给出球的运动方向。若球的速度取决于用户挥击的速度,那个么你们也需要追踪时间。另外,我不确定为什么要使力与物体的位置成比例:这意味着,如果物体在0,0,没有力,这可能不是你想要的。你只需要一个与轻弹矢量成比例的力。时间的长短将决定时间的长短。它看起来像这样:

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

这个问题似乎离题了,因为它缺少基本的代码示例。我的答案是第一个,代码逻辑几乎相同,但没有得分。我想,做我很糟糕。
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