2

我正在尝试在触摸事件上设置缩放功能。我使用 transition.to 和 onComplete 将父组移动到事件中心。但在某一时刻,它只是跳转到父组原点。有任何想法吗?

我现在正在粘贴代码的缩短版本。

local physics = require( "physics" )
physics.start()
physics.setContinuous( false )
--physics.setScale( 60 )



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



local backGround = display.newRect( 0,0,width,height )
backGround:setFillColor(91,91,91)

local allElements = display.newGroup()  
local grip = {}
local gripSize = {
-- w: gripwidth, h: gripheight, 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 function createTexture()
    local originX = 0
    local originY = height -75
    for i=0,50 do
        r = math.random(3)
        local x = originX + math.random(width)
        local y = originY - math.random(2*height)
        grip[i] = display.newRect( allElements, x, y, gripSize[r].w, gripSize[r].h)
        grip[i].size = gripSize[r].s
        if (r == 1) then
        grip[i]:setFillColor(51,255,0)
        elseif (r == 2) then
        grip[i]:setFillColor(255,51,51)
        elseif (r == 3) then
        grip[i]:setFillColor(51,51,255)
        end

    end
end 

createTexture()



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

head = display.newCircle( allElements, width/2,50,20 )
physics.addBody( head, { density=5, friction=0.5, bounce=0.3 } )


local touchBorder = 20

local function calcTouchOffset( e )
    local x, y = 0, 0

    if (e.x < touchBorder) then
        x = e.x - touchBorder
    elseif (e.x > width-touchBorder) then
        x = e.x - (width-touchBorder)
    end

    if (e.y < touchBorder) then
        y = e.y - touchBorder
    elseif (e.y > height-touchBorder) then
        y = e.y - (height-touchBorder)
    end

    return x, y
end


local function startDrag( e )
    local body = e.target
    local phase = e.phase
    local stage = display.getCurrentStage()

    if (e.phase == "began") then
        e.target.bodyType = "dynamic"
        e.target.hasFocus = true
        local x, y = allElements:contentToLocal( e.x, e.y )
        e.target.touchjoint = physics.newJoint( "touch", e.target, x, y )
        stage:setFocus( e.target )
        transition.to( allElements, { time = 200, x= -body.x, y= -body.y, xScale = 2, yScale = 2,})
        xOffset, yOffset = 0, 0
        return true
    elseif (e.target.hasFocus) then
        if (e.phase == "moved") then

            local x,y = allElements:contentToLocal(e.x, e.y)     -- This line is changed
            e.target.touchjoint:setTarget( x, y )               -- This line is changed
            xOffset, yOffset = calcTouchOffset( e )
        else
            transition.to( allElements, { time = 200, x = body.x, y = body.y, xScale = 1, yScale = 1,  })
            e.target.hasFocus = false
            e.target.touchjoint:removeSelf()
            e.target.touchjoint = nil
            stage:setFocus( nil )
            xOffset, yOffset = 0, 0

        end
        return true
    end
    xOffset, yOffset = 0, 0
    return false

end

head:addEventListener( "touch", startDrag )

    function allElements:update()   


    allElements.x, allElements.y = allElements.x - xOffset, allElements.y - yOffset
    allElements.x, allElements.y = allElements.x, allElements.y
    if (allElements.x > -startX) and ( startX < 0 ) then
        allElements.x = -startX
    elseif ( routeW < width ) then
        allElements.x = 0   
    elseif ( allElements.x < startX ) and ( startX < 0 ) then
        allElements.x = startX
    end
    --[[if (allElements.x > 0) then
        allElements.x = 0
    elseif ( routeW < width ) then
        allElements.x = 0       
    elseif ( allElements.x < (width-routeW) ) and ( routeW > width ) then
        allElements.x = width-routeW
    end ]]--    

    if (allElements.y > (routeH-height)) and ( routeH > height ) then
        allElements.y = routeH-height
    elseif ( routeH < height ) then
        allElements.y = 0
    elseif (allElements.y < 0) then
        allElements.y = 0
    end

end


function enterFrame()
    allElements:update()
end
4

1 回答 1

0

你只需要更新这部分:

elseif (e.target.hasFocus) then
    if (e.phase == "moved") then

        local x,y = allElements:contentToLocal(e.x, e.y)     -- This line is changed
        e.target.touchjoint:setTarget( x, y )               -- This line is changed
        xOffset, yOffset = calcTouchOffset( e )
    else
        transition.to( allElements, { time = 200, x = 0, y = 0, xScale = 1, yScale = 1,  })
        e.target.hasFocus = false
        e.target.touchjoint:removeSelf()
        e.target.touchjoint = nil
        stage:setFocus( nil )
        xOffset, yOffset = 0, 0

    end
    return true
end

通过将 x=body.x 替换为 x=0 并将 y=body.y 替换为 y=0

干杯:)

于 2013-10-19T15:20:15.917 回答