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
你能创建一个完整的例子吗?你有很多事情没有关系,如果你只有一个问题需要考虑,那么调试更容易。你的transition.to touch正在使用'x = -body.x',所以你把'allElements'移到'body.x'单元的左边。然后在完成时再次使用'x = -body.x',从而将'allElements'再次移动到左侧'body.x'单元。此外,在你的触摸释放处理程序中,你再次执行一次'transition.to',这将会破坏从初始触摸开始的任何已经运行的转换。 – six8
我将创建一个示例。我试图让效果像放大触摸事件发生的区域一样。这就是为什么在“开始”时,我将一个转换放入组中,并且放在第一组上。 x = -body.x用于聚焦缩放。否则,缩放将从组左上角的点开始。 – jumbee