2

我有一个分配给对象的事件侦听器。然后这会触发该startDrag功能。我在游戏中通过拖动来移动我的角色。但是一旦我滚动或移动显示器,我的拖动功能就会变得一团糟。它没有做出应有的反应,并且event.target根本无法控制 object()。

其他人有过这种经历吗?

这是电晕虫吗?

我该如何解决这个问题?

我可以startDrag在滚动时暂时禁用事件侦听器吗?或者也许重新启动它?

所有帮助将不胜感激。

这是代码...

local physics = require( "physics" )
physics.start()
physics.setContinuous( false )
display.setStatusBar( display.HiddenStatusBar ) 

physics.setScale( 60 )
physics.setDrawMode( "hybrid" )

local height = display.contentHeight
local width = display.contentWidth

local allElements = display.newGroup()
local texsGroup = display.newGroup()



local backGround = display.newRect(0,0-height,width,2*height)
backGround:setFillColor(91,91,91)
backGround:toBack()

local wallBottom = display.newRect(texsGroup, 0,height-20,width,20)
physics.addBody(wallBottom, "static", { density=5, friction=0.5, bounce=0.3 } )

local tex = {}
local numberRips = 60

local texSize = {
-- w: texwidth, h: texheight, s: strength required
{w=30, h=20, s=1},
{w=20, h=10, s=1.5},
{w=10, h=10, s=2},
}

local r
local lim = display.newGroup()
local function createRips()
local originX = 0
local originY = height -75

for i=0,numberRips do
    r = math.random(3)
    local x = originX + math.random(width)
    local y = originY - math.random(2*height)
    tex[i] = display.newRect(lim, x, y, texSize[r].w, texSize[r].h)
    tex[i].status = "active"
    tex[i].size = texSize[r].s
    if (r == 1) then
    tex[i]:setFillColor(51,255,0)
    elseif (r == 2) then
    tex[i]:setFillColor(255,51,51)
    elseif (r == 3) then
    tex[i]:setFillColor(51,51,255)
    end
end
end 

createRips()

local w, h, r = width/2, height - 265, 12

local L = display.newCircle(w-115,h+29,r)


local buttonRadius = 35
local button3 = display.newCircle((L.x),(L.y),buttonRadius)
button3.myName = "L"

allElements:insert(button3)
allElements:insert(lim)
allElements:insert(L)

local d, f, b = 15, 1, 0.15


physics.addBody(L, "dynamic", { density=d, friction=f, bounce=b,  radius=r } )
button3.isVisible = false
button3.isHitTestable = true
physics.addBody( button3, "static", { density=1, radius=buttonRadius } )

local function addFrictionJoint(a, b, posX, posY, lowerAngle, upperAngle, mT) 
local j = physics.newJoint ( "pivot", a, b, posX, posY, rFrom, rTo)
j.isLimitEnabled = true
j:setRotationLimits (lowerAngle, upperAngle)
return j
end

-- JOINTS
addFrictionJoint( button3, L, L.x, L.y, 0, 0 )




local function startDrag( event, params )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
local direction = event.direction


if "began" == phase then
    stage:setFocus( body, event.id )
    body.isFocus = true
    event.target.bodyType = "dynamic"
    -- Create a temporary touch joint and store it in the object for later reference
    if params and params.center then
        -- drag the body from its center point
        body.tempJoint = physics.newJoint( "touch", body, body.x, body.y )
    else 
        -- drag the body from the point where it was touched
        body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
    end

    --body.tempJoint.maxForce = 0.25*body.tempJoint.maxForce
    -- Apply optional joint parameters
    if params then
        local maxForce, frequency, dampingRatio

        if params.maxForce then
            -- Internal default is (1000 * mass), so set this fairly high if setting manually
            body.tempJoint.maxForce = params.maxForce
        end

        if params.frequency then
            -- This is the response speed of the elastic joint: higher numbers = less lag/bounce
            body.tempJoint.frequency = params.frequency
        end

        if params.dampingRatio then
            -- Possible values: 0 (no damping) to 1.0 (critical damping)
            body.tempJoint.dampingRatio = params.dampingRatio
        end
    end

elseif body.isFocus then
    if "moved" == phase then

        -- Update the joint to track the touch
        body.tempJoint:setTarget( event.x, event.y )



    elseif "ended" == phase or "cancelled" == phase then
        stage:setFocus( body, nil )
        body.isFocus = false
        -- Remove the joint when the touch ends         
        body.tempJoint:removeSelf()
        body.bodyType = "static"
    end
end

-- Stop further propagation of touch event
return true

end

function moveCamera(e)
if button3.y < -lim.y + 300 then 
    allElements.y = -(button3.y - 300)

end 
end
Runtime:addEventListener("enterFrame", moveCamera)

button3:addEventListener( "touch", startDrag )
4

1 回答 1

0

当您滚动屏幕时,触摸事件和物理对象位于不同的坐标系中。见 localToContent/globalToContent

于 2014-05-19T16:35:23.070 回答