我试图让许多盒子根据触摸输入像摩天轮一样旋转
所以想象一下屏幕上的摩天轮,向下拖动其中一个手推车会使摩天轮单向旋转,向上拖动会使轮子反向旋转
摩天轮上的每个盒子或“手推车”都不会旋转,它们只会做圆周运动,就像摩天轮一样
我现在几乎可以正常工作了,但是我的盒子正在从初始抓取点迅速脱离,因此当我触摸其中一个盒子时,它会很快出现在其他地方,但随后正常旋转,我希望它从初始抓取点继续平稳旋转
下面是我当前的代码
local squares = display.newGroup()
local square = display.newRect(0,0,200,200)
square.x, square.y = 320, 320
square:setFillColor(100,255,55)
squares:insert(square)
local square2 = display.newRect(0,0,200,200)
square2.x, square2.y = 320, 320
square2:setFillColor(999,255,55)
squares:insert(square2)
local function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
local degrees = event.y
local rads = degrees * (math.pi / 360.0)
square.x = 300 * math.cos(rads) + 500
square.y = 300 * math.sin(rads)+ 500
degrees = degrees + 100
print (square.x, square.y)
local rads2 = degrees * (math.pi / 360.0)
square2.x = 300 * math.cos(rads2)+ 500
square2.y = 300 * math.sin(rads2)+ 500
degrees = degrees - 100
print (square.x, square.y)
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
squares:addEventListener( "touch", onTouch )
请随时指出我犯的任何愚蠢的错误,如果你能告诉我如何获得相同的效果,但围绕另一个对象而不是一个点,我会非常感激,谢谢