Pixel Envision Ltd.
Mobile Game Development Studio
  • Home
  • About Us
  • Our Games
    • Hyper-Casual
    • Casual Games
      • Don’t Get Caught
      • Hordes of Enemies
      • noded
      • kubic
      • Whip Swing
      • Witches’ Brew
    • Kids Apps
      • Coloring Book
      • Cars & Trucks Puzzle
      • Train Puzzles for Kids
      • Animal Puzzle
      • Fairy Tale Puzzles for Kids
      • Find the Differences
  • Support
  • Privacy Policy
Select Page ...

CoronaSDK Proper Orientation Rotation with Animation

CoronaSDK Proper Orientation Rotation with Animation

August 3, 2011 CoronaSDK, Lua

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…

rotationfix.lua

Lua
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 )

← Welcome to Pixel Envision!
CoronaSDK Enhanced UI Library (ui.lua) →

10 Responses to CoronaSDK Proper Orientation Rotation with Animation

  • Emanouel
    9 / 27 / 2012

    im also having trouble getting this working with ghost vs monsters demo..

    currently in my game i have it set up as

    settings =
    {
    orientation = {
    default = “landscapeRight”, },
    iphone =
    {
    plist =
    {
    CFBundleIconFile = “Icon.png”, CFBundleIconFiles = { “Icon.png”,
    “Icon@2x.png”,
    “Icon-72.png”,

    — “Icon-72@2x.png”, “Icon-Small.png”, “Icon-Small@2x.png”, “Icon-Small-50.png”, “Icon-Small-50@2x.png”,
    },
    },
    },
    }

    can you send me a sample to try out ??

    thanks

    Emanouel 9 / 27 / 2012
    • Admin
      9 / 28 / 2012

      I’m afraid I won’t be able to help you with the sample as we are no longer using CoronaSDK. But you should try to add following config directives so orientation section should read:
      default = landscapeRight,
      content = landscapeRight,
      supported =
      {
      landscapeLeft, landscapeRight
      },

      I hope this helps…

      Admin 9 / 28 / 2012
  • Ezra
    1 / 6 / 2012

    in landscape mode i have an issue when i rotate the ipad in the right or left direction. each time on the first rotation the content is updsidedown, on the second it looks good & then again on the third upsidesdown…
    i have set landscapeRight & left in both files build.settings & rotationfix.lua as described. did i have put something wrong ? thank you.

    Ezra 1 / 6 / 2012
    • Admin
      1 / 11 / 2012

      Hi Ezra,

      Have you also put “content” tag in there? That sounds like rotationfix & native rotation working together…

      build.settings should look like that ->

      orientation =
      {
      default = landscapeLeft,
      content = landscapeLeft,
      supported =
      {
      landscapeLeft, landscapeRight
      }
      },

      Admin 1 / 11 / 2012
  • Luciane
    11 / 30 / 2011

    I can’t make it work in the Corona simulator in landscape mode in my game.
    I tried on Ghosts versus Monsters example and it also does not work.
    Does it work for you?

    Luciane 11 / 30 / 2011
    • Admin
      11 / 30 / 2011

      Hi Luciane,

      I’ve just replied your ticket and sent u a working Ghost vs Monsters sample project…

      Admin 11 / 30 / 2011
  • Mike
    9 / 7 / 2011

    Works great on iPad 2. Looks more professional.

    Thank you,
    Mike

    Mike 9 / 7 / 2011
    • Admin
      9 / 8 / 2011

      You’re welcome… I’m glad to hear it’s been useful. :)

      Admin 9 / 8 / 2011
    • Corona Starter
      7 / 21 / 2014

      Hi,

      I want to Make the game like some pages are in portrait mode and some are in landscape mode, Using rotation i am not able approach it .
      So, please suggest me some tutorials.

      Thankyou..

      Corona Starter 7 / 21 / 2014
    • Erdener Gonenc
      7 / 23 / 2014

      Hi Nirosha, I’m afraid I won’t be able to help you as we’re not using Corona SDK anymore. You should try Coronalabs forum instead…

      Erdener Gonenc 7 / 23 / 2014
  • Tags

    3ds Max Coming Soon CoronaSDK Featured Flash Lua MAXScript PHP Programming Reviews Tips & Tricks Unity 3D Windows Phone
  • Recent Comments

    • Yogesh Singh on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Admin on Maxscript – Vray Cubemap Generator for Unity
    • charlie on Maxscript – Vray Cubemap Generator for Unity
    • Mastan on PHP Currency Converter
    • Rakesh Vishnoi on ZIP (POSTAL) Code Validation Regex & PHP code for 12 Countries
    • Find us on

      amazonandroidapplefacebooklinkedintwitterwindowsyoutube
    • Company Information

      Pixel Envision Limited is a company registered in England, company number: 09558675. Registered Office: Preston Park House, South Road, Brighton, East Sussex, BN1 6SB, United Kingdom

    • Privacy Policy
    Copyright © 2011-2020 Pixel Envision Ltd, all rights reserved.