【问题标题】:Making object move to a touch position - Corona SDK使对象移动到触摸位置 - Corona SDK
【发布时间】:2013-07-22 12:54:30
【问题描述】:

我正在尝试让游戏对象移动到触摸位置。我尝试了许多不同的方法来做到这一点,但都无济于事。我每帧都在更新对象的 y 位置以保持它不断向前移动,我不确定这是否会影响添加力。

这是我移动乌鸦的地方(在更新函数中):

crow:translate((platformSpeed),0)

这是我试图让乌鸦移动到触摸位置的地方:

        local function attack(xPos,yPos)

            --get magnitude of touch vector
            local magnitude = math.sqrt(xPos*2 + yPos*2)

            --normalize vector
            xPos = xPos / magnitude
            yPos = yPos / magnitude

            print(xPos..yPos)

            local forceMag = 0.1 -- change this value to apply more or less force
            --now apply the force
            crow:setLinearVelocity(xPos*forceMag, yPos*forceMag)
            --crow:applyLinearImpulse(xPos*forceMag, yPos*forceMag, crow.x, crow.y)
        end

        local function touchHandler(event)

            if (event.phase == "began") then
            end

            if (event.phase == "ended") then
                if (event.yStart>event.y+10) then
                    jump()
                else attack(event.x,event.y) end
            end

        end

如你所见,我一直在尝试线速度和线冲量,但方向总是错误的!

任何帮助都会很棒,谢谢! 艾伦。

【问题讨论】:

标签: physics game-physics coronasdk


【解决方案1】:

试试这个:

local cow = display.newRect(0,0,50,50)  -- create your object

local trans
local function moveObject(e)
    if(trans)then
      transition.cancel(trans)
    end
    trans = transition.to(cow,{time=200,x=e.x,y=e.y})  -- move to touch position
end
Runtime:addEventListener("tap",moveObject)

继续编码........ :)

【讨论】:

    【解决方案2】:

    这与最近的问题类似。

    我对最近的问题有一个答案,你可以检查并在一个空白项目上运行我的示例代码,看看它是如何工作的。

    我的代码使用物理和线速度。

    https://stackoverflow.com/a/17847475/1605727

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 2016-05-20
      • 2013-04-01
      • 2012-02-26
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      相关资源
      最近更新 更多