【问题标题】:Corona SDK rotationCorona SDK 轮换
【发布时间】:2013-02-19 04:40:57
【问题描述】:

我正在研究一个涉及时钟和 set_alarm 机制的项目,该机制通过拖动连接到时钟中心的旋转指针(时钟是经典的,就像带有指针的模拟,而不是数字)来工作,所以通过旋转指针(即设置闹钟)可以设置闹钟。以下是出处:

local function rotateObj2(event)
    local t = event.target
    local phase = event.phase

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

            -- Store initial position of finger
            t.x1 = event.x
            t.y1 = event.y

    elseif t.isFocus then
            if (phase == "moved") then
                    animateAlarm = true
                    t.x2 = event.x
                    t.y2 = event.y

                    angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
                    angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
                    print("angle1 = "..angle1)
                    rotationAmt = angle1 - angle2

                    --rotate it
                    t.rotation = t.rotation - rotationAmt
                    print ("t.rotation = "..t.rotation)

                    t.x1 = t.x2
                    t.y1 = t.y2
                    alarm.rotation = t.rotation
                    setAlarm.rotation = t.rotation
                    print ("setAlarm.rotation = "..t.rotation)
                    if(alarm.rotation >=0)then
                        local hourValue = math.floor(t.rotation/30)
                        local minuteValue = math.floor(t.rotation*2)
                        local hour = hourValue
                        local minute = minuteValue  

                        if(minuteValue>=720 and minuteValue<780)then 
                            minute = minuteValue-720
                        elseif(minuteValue>=660 and minuteValue<720)then 
                            minute = minuteValue-660
                        elseif(minuteValue>=600 and minuteValue<660)then 
                            minute = minuteValue-600
                        elseif(minuteValue>=540 and minuteValue<600)then 
                            minute = minuteValue-540
                        elseif(minuteValue>=480 and minuteValue<540)then 
                            minute = minuteValue-480
                        elseif(minuteValue>=420 and minuteValue<480)then 
                            minute = minuteValue-420
                        elseif(minuteValue>=360 and minuteValue<420)then 
                            minute = minuteValue-360
                        elseif(minuteValue>=300 and minuteValue<360)then 
                            minute = minuteValue-300
                        elseif(minuteValue>=240 and minuteValue<300)then 
                            minute = minuteValue-240
                        elseif(minuteValue>=180 and minuteValue<240)then 
                            minute = minuteValue-180
                        elseif(minuteValue>=120 and minuteValue<180)then 
                            minute = minuteValue-120
                        elseif(minuteValue>=60 and minuteValue<120)then 
                            minute = minuteValue-60
                        end

                        if (hour < 10) then hour = "0" .. hour end
                        if (minute < 10) then minute = "0" .. minute end


                        hourField.text = hour
                        minuteField.text = minute

                    end
            elseif (phase == "ended") then
                    display.getCurrentStage():setFocus( nil )
                    t.isFocus = false


                    print ("ENDEDsetAlarm.rotation = "..setAlarm.rotation)
            end
    end

    -- Stop further propagation of touch event
    return true
end

问题是,当我尝试定义闹钟值(如 03.45)时,没问题。如您所见,我转换了指针的旋转。但是,如果旋转低于 -90 或超过 270,我不会得到结果。那是因为我没有定义如何处理那个(-旋转)

问题:

一段时间后,旋转发生变化。所以我没有旋转 = 0 到 270 和 -90 到 0,但是 -90 到 -360+(-90) = -450;

有时会变成 60 到 360+60=420

这是什么鬼?! =(

请帮忙,我无法解决这个问题,因为这些“转变”似乎是随机发生的(或者我只是不明白为什么会发生)

【问题讨论】:

    标签: rotation coronasdk


    【解决方案1】:

    你可以试试这个:其中 dx 和 dy 等于 x 的变化和 y 的变化

    angle = ((math.atan2(dx,dy)*180 / math.pi) - 180) * -1
    

    无论您转向哪个方向或旋转多少次,这都会为您提供 0 到 360 之间的正值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 2017-03-14
      • 2019-12-19
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多