【问题标题】:rotating object around point or other object using touch corona sdk使用 touch corona sdk 围绕点或其他对象旋转对象
【发布时间】:2013-05-22 20:11:16
【问题描述】:

我正在尝试让一些框根据触摸输入像摩天轮一样旋转

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

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

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

下面是我当前的代码

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 )

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

【问题讨论】:

    标签: rotation touch coronasdk drag orbit


    【解决方案1】:

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

    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)
    

    我花了很多时间。

    【讨论】:

    • 感谢您的回复,我已经尝试过您的方法,但它仍然存在与我的代码相同的捕捉问题,因此,只要我触摸一个方块拖动,方块就会出现在别处然后携带照常进行
    • 您是否尝试过我发布的确切代码。我在模拟器上对其进行了测试,它没有任何捕捉问题。
    • 我改进了代码。您仍然应该修复捕捉问题,但现在应该很容易完成。记得接受并投票我的答案。
    • 再次感谢您的反馈,但是如果您阅读了我最初的帖子,我的主要问题是捕捉问题,所以很遗憾,我不能接受您的回答,因为您的方法给出了相同的结果
    • 我花了大约一个小时,但我做到了!现在我明白了。这很容易。问题是您的初始代码让我感到困惑。请参阅新代码。 记得接受我的回答并投票
    【解决方案2】:

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

    degrees = Math.atan(event.y / event.x)
    

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 2021-09-13
      • 2020-11-16
      • 2015-07-14
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2017-02-10
      相关资源
      最近更新 更多