0

我试图让许多盒子根据触摸输入像摩天轮一样旋转

所以想象一下屏幕上的摩天轮,向下拖动其中一个手推车会使摩天轮单向旋转,向上拖动会使轮子反向旋转

摩天轮上的每个盒子或“手推车”都不会旋转,它们只会做圆周运动,就像摩天轮一样

我现在几乎可以正常工作了,但是我的盒子正在从初始抓取点迅速脱离,因此当我触摸其中一个盒子时,它会很快出现在其他地方,但随后正常旋转,我希望它从初始抓取点继续平稳旋转

下面是我当前的代码

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 )

请随时指出我犯的任何愚蠢的错误,如果你能告诉我如何获得相同的效果,但围绕另一个对象而不是一个点,我会非常感激,谢谢

4

2 回答 2

2

这是一个完整的工作示例。

local squares = display.newGroup()

local wheelX =  display.contentCenterX
local wheelY =  display.contentCenterY

local radius = 220
local degrees = -180
local squareH = 150

local square = display.newRect(0,0,squareH,squareH)
square:setFillColor(255,255,255)
square.degStart = 100
squares:insert(square)

local square2 = display.newRect(0,0,squareH,squareH)
square2:setFillColor(999,255,55)
square2.degStart = -10
squares:insert(square2)


local function drawRects(degrees)
    local rads = (square.degStart + degrees) * (math.pi / 180.0)
    square.x = radius * math.cos(rads) + wheelX
    square.y = radius * math.sin(rads) + wheelY

    local rads2 = (square2.degStart + degrees) * (math.pi / 180.0)
    square2.x = radius * math.cos(rads2) + wheelX
    square2.y = radius * math.sin(rads2) + wheelY
end

local function getDegrees(square)
    local x = square.x
    local y = square.y

    local degrees = math.atan2((y - wheelY) , (x - wheelX)) * (180 / math.pi)

    return degrees
end


local function onTouch( event )
    local t = event.target

    local phase = event.phase
    local parent = t.parent

    if "began" == phase then
        print("began")
        parent:insert( t )
        display.getCurrentStage():setFocus( t )
        t.isFocus = true

        square.degStart = getDegrees(square)
        square2.degStart = getDegrees(square2)
    elseif t.isFocus then
        if "moved" == phase then
            degrees = math.atan2((event.yStart - wheelY) , (event.xStart - wheelX)) * (180 / math.pi)

            degrees2 = math.atan2((event.y - wheelY) , (event.x - wheelX)) * (180 / math.pi)

            diffDegrees = degrees2 - degrees
            drawRects(diffDegrees)

            print("diffDegrees: " .. diffDegrees)
        elseif "ended" == phase or "cancelled" == phase then
            display.getCurrentStage():setFocus( nil )
            t.isFocus = false
        end
    end
return true
end

squares:addEventListener( "touch", onTouch )

drawRects(degrees)

我花了很多时间。

于 2013-05-23T15:54:51.020 回答
1

您应该尝试获取 event.y 和 event.x,然后使用公式检索度数:

degrees = Math.atan(event.y / event.x)
于 2013-05-24T20:47:23.317 回答