试图让用户按照用户滑动屏幕的方向移动:Corona SDK

时间:2013-07-10 21:26:20

标签: game-physics corona

我刚刚开始使用基本的Corona SDK项目,我遇到了一些障碍。我已经使对象和菜单变得很好,甚至为游戏添加了重力,但我想知道当用户刷过屏幕时是否有一个简单的方法让对象移动,并且对象将朝着那个方向移动。

我们非常感谢任何帮助,我的对象的代码低于

    -- make a Chameleon 
local Chameleon = display.newImageRect( "Chameleon.png", 70, 70 )
Chameleon.x= 50
Chameleon.y= 440
    physics.addBody(Chameleon, "dynamic", {density=.1, bounce=.1, friction=.2, radius=12})

function touchScreen(event)
  -- print("touch")
end

Runtime:addEventListener("touch", touchScreen)

1 个答案:

答案 0 :(得分:3)

如果你想用线速度实现可拖动对象,你可以参考下面的代码只需复制代码并制作新项目

local physics = require( "physics" )
physics.start()

local Rect = display.newRect(30,30,30,30)
Rect:setFillColor(255,0,0)

local flooring = display.newRect(0,display.contentHeight/1.1, display.contentWidth, 10)
    physics.addBody(Rect,"dynamic")
    physics.addBody(flooring,"static")

    local activateDash = false
    local bx = 0
    local by = 0

    heroTouch = function(event)
        if Rect then
            if event.phase == "began" then
                bx = event.x
                by = event.y
            elseif event.phase == "moved" then
                activateDash = true
            elseif event.phase == "ended" then
                if activateDash then
                    if _G.gX == 0 and _G.gY ~= 0 then
                        Rect:setLinearVelocity(event.x-bx,0)
                    elseif _G.gX ~= 0 and _G.gY == 0 then
                        Rect:setLinearVelocity(0,event.y-by)
                    else
                        Rect:setLinearVelocity(event.x-bx,event.y-by)
                    end
                    activateDash = false
                end
            end
        end
    end
Runtime:addEventListener("touch",heroTouch)

希望这可以提供帮助