Lua 触摸事件上的缩放
我正在尝试在触摸事件上设置缩放功能。我使用transition.to和onComplete缩放父组将其移动到事件中心。但在某一点上,它只是跳转到父组原点。有什么想法吗 我现在正在粘贴代码的缩短版本Lua 触摸事件上的缩放,lua,touch,zooming,coronasdk,Lua,Touch,Zooming,Coronasdk,我正在尝试在触摸事件上设置缩放功能。我使用transition.to和onComplete缩放父组将其移动到事件中心。但在某一点上,它只是跳转到父组原点。有什么想法吗 我现在正在粘贴代码的缩短版本 local physics = require( "physics" ) physics.start() physics.setContinuous( false ) --physics.setScale( 60 ) local height = display.contentHeight lo
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
本地物理=要求(“物理”)
物理开始
物理.设置连续(假)
--物理.设置刻度(60)
本地高度=display.contentHeight
本地宽度=display.contentWidth
本地背景=display.newRect(0,0,宽度,高度)
背景:setFillColor(91,91,91)
本地等位基因=display.newGroup()
局部夹点={}
局部灰色={
--w:gripwidth,h:gripheight,s:所需强度
{w=30,h=20,s=1},
{w=20,h=10,s=1.5},
{w=10,h=10,s=2},
}
局部r
局部函数createTexture()
局部原点=0
局部原点=高度-75
对于i=0,50 do
r=数学随机(3)
局部x=原始x+数学随机(宽度)
局部y=原始-数学随机(2*高度)
grip[i]=display.newRect(等位基因,x,y,gripSize[r].w,gripSize[r].h)
夹点[i].size=gripSize[r].s
如果(r==1),则
夹点[i]:setFillColor(51255,0)
elseif(r==2)则
夹点[i]:setFillColor(255,51,51)
elseif(r==3)则
夹点[i]:setFillColor(51,51255)
结束
结束
结束
createTexture()
wallBottom=display.newRect(等位基因,0,高度-20,宽度,20)
物理学.addBody(墙底,“静态”{密度=5,摩擦=0.5,反弹=0.3})
头部=显示。新圆圈(等位基因,宽度/2,50,20)
物理。addBody(头部,{密度=5,摩擦=0.5,反弹=0.3})
本地接触边界=20
局部函数calcTouchOffset(e)
局部x,y=0,0
如果(e.x宽度触摸边框)然后
x=e.x-(宽度触摸边框)
结束
如果(e.y<触摸边界),则
y=e.y-触摸边界
elseif(e.y>高度触摸边框)然后
y=e.y-(高度接触边界)
结束
返回x,y
结束
本地功能启动图(e)
局部体=e.target
局部相位=e相位
本地阶段=显示。getCurrentStage()
如果(e.阶段==“开始”),则
e、 target.bodyType=“动态”
e、 target.hasFocus=true
局部x,y=等位基因:内容局部(e.x,e.y)
e、 target.touchjoint=physics.newJoint(“touch”,e.target,x,y)
阶段:设置焦点(如目标)
到(等位基因,{time=200,x=-body.x,y=-body.y,xScale=2,yScale=2,})
xOffset,yOffset=0,0
返回真值
elseif(e.target.hasFocus)然后
如果(e.相位==“移动”),则
局部x,y=等位基因:内容局部(e.x,e.y)--这条线改变了
e、 target.touchjoint:setTarget(x,y)——此行已更改
xOffset,yOffset=calcTouchOffset(e)
其他的
到(等位基因,{time=200,x=body.x,y=body.y,xScale=1,yScale=1,})
e、 target.hasFocus=false
e、 target.touchjoint:removeSelf()
e、 target.touchjoint=nil
阶段:设置焦点(无)
xOffset,yOffset=0,0
结束
返回真值
结束
xOffset,yOffset=0,0
返回错误
结束
标题:addEventListener(“触摸”,起始标签)
函数等位基因:更新()
等位基因.x,等位基因.y=等位基因.x-xOffset,等位基因.y-yOffset
等位基因.x,等位基因.y=等位基因.x,等位基因.y
如果(等位基因.x>-startX)和(startX<0),则
等位基因.x=-startX
elseif(routeW<宽度)然后
等位基因x=0
elseif(等位基因x0),则
等位基因x=0
elseif(routeW<宽度)然后
等位基因x=0
elseif(等位基因.x<(宽度routeW))和(routeW>宽度),然后
等位基因.x=宽度routeW
完]]--
如果(等位基因.y>(routeH高度))和(routeH>高度),则
等位基因。y=路径高度
elseif(路径<高度)然后
等位基因。y=0
elseif(等位基因y<0)则
等位基因。y=0
结束
结束
函数enterFrame()
等位基因:更新()
结束
您只需更新此部分:
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
干杯:)您能否创建一个可以运行的完整示例?你有很多不相关的事情要做,如果你只有一个问题要看,那么调试就容易多了。您的.to on-touch转换使用的是
x=-body.x
,因此您将等位基因
向左移动body.x
单位。然后在完成时再次使用x=-body.x
,从而将等位基因
再次移到左侧body.x
单位。另外,在触摸释放处理程序中,您再次执行转换。到
,这将打乱从初始触摸开始的任何已经运行的转换。我将创建一个示例。我试着制作一个效果,比如放大触摸事件发生的区域。这就是为什么在“开始”时我放了一个过渡,在发布时我缩小了团队规模。x=-body.x用于聚焦缩放。否则,将从组的左上角点开始缩放。