【问题标题】:Attempting to have an object move in the direction user swipes the screen: Corona SDK尝试让对象向用户滑动屏幕的方向移动:Corona SDK
【发布时间】:2013-07-10 21:26:19
【问题描述】:

我刚刚开始使用一个基本的 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)

【问题讨论】:

    标签: game-physics coronasdk


    【解决方案1】:

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

    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)
    

    希望对你有帮助

    【讨论】:

    • 非常感谢,这很好用!当我还有你的时候,如果你不介意的话,如果我想让物体停止并挂在某个点上(在被甩到那个点之后),你能告诉我可能是什么方法,或者其他例子。
    • 如果对象与另一个对象发生碰撞或到达某个点,您究竟希望对象如何停止在特定位置?顺便说一句,您可以对答案进行投票以使其成为其他人的参考
    • 如果它要继续到某个点。我会投票赞成,但我没有足够的声望点。遗憾的是只有 3 个。
    • 您可以检测 Rect 的 x 和 y 轴,当它到达您希望它停止的某个点时,使 Rect 的物理体静态
    • 如果你想让物理体停止下落,你也可以移除它
    猜你喜欢
    • 2014-08-02
    • 2012-07-05
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多