方向旋转有时会偏离角度

时间:2014-02-12 06:28:14

标签: lua corona

我正在从一本书中学习,并且正在改变方向。

我不明白为什么会这样:当我通过Corona Simulator测试我的应用时,如果我快速地在屏幕上旋转我的对象。它的角度很古怪。

这是我的代码:

local portrait = display.newText("Portrait", display.contentWidth/2, display.contentHeight/2, native.systemFont, 24) 
local landscape = display.newText("Landscape", display.contentWidth/2, display.contentHeight/2, native.systemFont, 24) 
portrait:setFillColor(1, 1, 1)
portrait.alpha = 1 
landscape:setFillColor(1, 1, 1) 
landscape.alpha = 0 
local function onOrientationChange (event) 
    if (event.type =="landscapeRight" or event.type == "landscapeLeft") then 
        local newAngle = landscape.rotation - event.delta 
        transition.to( landscape, {time= 150, rotation = newAngle}) 
        transition.to( portrait, {rotation = newAngle}) 
        portrait.alpha = 0 
        landscape.alpha = 1
    else 
        local newAngle = portrait.rotation - event.delta
        transition.to( portrait, {time= 150, rotation = newAngle})
        transition.to( landscape, {rotation = newAngle})
        portrait.alpha = 1
        landscape.alpha = 0
    end
end 

Runtime:addEventListener( "orientation", onOrientationChange )

1 个答案:

答案 0 :(得分:2)

这种情况的发生主要是由于transitions正在进行中。所以,你必须在调用另一个之前停止某些转换。所以:

首先初始化一个包含转换的数组,位于function onOrientationChange (event)

之上
  local trans = {}

然后命名所有过渡:

  -- Inside 1st if --
  trans[1] = transition.to( landscape, {time= 150, rotation = newAngle})
  trans[2] = transition.to( portrait, {rotation = newAngle})
  ...
  ...
  --Inside 2nd if --
  trans[3] = transition.to( portrait, {time= 150, rotation = newAngle})
  trans[4] = transition.to( landscape, {rotation = newAngle})

然后就在线下:local newAngle = landscape.rotation - event.delta,停止所有过渡, 并将旋转直接设置为值= newAngle

  -- Inside 1st if (just above 'trans[1] = transition.to...')--
  for i=1,4 do if(trans[i])then transition.cancel(trans[i]) end end
  portrait.rotation = newAngle
  ...
  ...
  --Inside 2nd if  (just above 'trans[3] = transition.to...')--
  for i=1,4 do if(trans[i])then transition.cancel(trans[i]) end end
  landscape.rotation = newAngle

注意:这里我在调用另一个之前取消了所有转换。通常,您只需要取消正在进行的转换。

保持编码...............:)