2013-05-30 59 views
2

我有一個事件監聽器分配給一個對象。然後觸發startDrag函數。我正在拖動我的角色在遊戲中移動。但只要我滾動或移動顯示器,我的拖動功能就會搞砸了。它沒有反應,因爲它應該和對象(event.target)根本無法控制。在Corona中滾動和拖動 - > BUG?

還有其他人有過這樣的經歷嗎?

這是一個電暈蟲嗎?

我該如何解決這個問題?

我可以在滾動時臨時禁用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) 
+0

您是否使用觸摸對象的.isFocus參數? –

+2

您應該添加一些示例代碼。也許你做錯了什麼。 – vovahost

+0

我們可以添加代碼 – DevfaR

回答

0

當您滾動屏幕上的觸摸事件和物理對象在不同的​​座標系。請參閱localToContent/globalToContent