This is a code I came up when learning Lua & CoronaSDK programming. Copy of the original code still available at Ansca code exchange.
It is mostly working as I wanted except a small bug. If you rotate the device while app is loading assets, there might be a small delay. I’m planning to work on an update for that as soon as our game is out…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
--]] -- SET YOUR ORIENTATIONS HERE local mainOr = "portrait" local altOr = "portraitUpsideDown" -- MASTER CODE local stage = display.getCurrentStage( ) stage:setReferencePoint(display.CenterReferencePoint) isRotated = false local rotatefilter --function reserve local rotatestart --function reserve local rotatecomplete --function reserve local curOrientation = mainOr --inital launch always match mainOr local rota = 0 local isrotating = false local rd = 600 -- iPad rotates slower if system.getInfo("model") == "iPad" then rd = 750 end function rotatefilter( ) if (system.orientation == mainOr or system.orientation == altOr ) then return system.orientation else if curOrientation then return curOrientation else return mainOr end end end function rotatestart( val, initial ) if isrotating == false and curOrientation ~= val then if val == mainOr then rota = 180 curOrientation = mainOr elseif val == altOr then rota = -180 curOrientation = altOr end isrotating = true if initial and rd == 750 then transition.to( stage, { rotation=rota, time=0, delta=true, onComplete=rotatecomplete } ) else transition.to( stage, { rotation=rota, time=rd, delta=true, transition=easing.inOutQuad, onComplete=rotatecomplete } ) end end end function rotatecomplete( ) isrotating = false if curOrientation == altOr then isRotated = true else isRotated = false end if curOrientation ~= rotatefilter() then rotatestart(rotatefilter()) end end --Check initial orientation and and rotate if needed if ( system.orientation == altOr and isrotating == false) then rotatestart(altOr,true) end local function onOrientationChange( event ) local type = event.type if ( type == mainOr or type == altOr ) then rotatestart(type) end end Runtime:addEventListener( "orientation", onOrientationChange ) |