After my picture book app is almost done, I got an idea to make a little more: to make the illustration/animation of each page to scale with the screen width of the device. I planed this to show entire composition of each illustration with animation in each page. Fortunately, I organized the illustration and animation in one display group. Here is the structure of my app:
Somehow I made the content of my app as 960x 640, and scale = “zoomEven” in the year of 2017. Each illustration with animation is 640x 640, align on the top. For the idea of scaling illustration according to the device resolution, I wrote a deviceRatio() function in main.lua
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- include the Corona "composer" module local composer = require("composer") -- hide the status bar display.setStatusBar( display.HiddenStatusBar ) display.setDefault("background", 1, 0.6823, 0.78823) -- light pink BG -- Removes bottom bar on Android https://docs.coronalabs.com/api/library/native/setProperty.html if system.getInfo( "androidApiLevel" ) and system.getInfo( "androidApiLevel" ) \< 19 then native.setProperty( "androidSystemUiVisibility", "lowProfile" ) else native.setProperty( "androidSystemUiVisibility", "immersiveSticky" ) end -- get resolution scaleFactor for illustration function deviceRatio() local ratio = display.pixelHeight/display.pixelWidth local scaleFactor if ratio \<=1.5 then scaleFactor = 1 elseif ratio == 1.6 then scaleFactor = 0.9375 elseif ratio == 1.666 then scaleFactor = 0.9 elseif ratio \>1.7 and ratio \<= 1.8 then scaleFactor = 0.846 elseif ratio \>2 then scaleFactor = 0.6927 end print("scaleFactor in main", scaleFactor) return scaleFactor end local scaleFactor = deviceRatio() composer.setVariable("scaleFactor", scaleFactor) print("scaleFactor in main II", scaleFactor) -- load title/cover screen composer.gotoScene( "coverPage", "fade", 300)
I get the scaleFactor and pass it to page01.lua to scale the display group which contains illustration and animation of each page. Everything worked perfectly on Corona Simulator as I switched among different resolution and devices. However, after I made a build and ran on my Android phone, it showed a Runtime Error right away when I touch the coverPage.
Runtime Error
C:\ …\picBookProject\page01.lua:200: bad argument#1 to ‘scale’ (number expected, got nil)
-- page01.lua -- ----------------------------------------------------------------------------------- local composer = require( "composer" ) local widget = require( "widget" ) local scene = composer.newScene() local pages = require("data") -- load the pages "module" of page information ... local backGroup -- add mainImage(illustration #.png) in, otherwise mainImage are keeping in front of buttons local mainGroup local animGroup -- one illustration with animation local uiGroup ... -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- Set up display groups backGroup = display.newGroup() sceneGroup:insert( backGroup ) local illuScale = composer.getVariable("scaleFactor") print("illuScale", illuScale) -- to confirm that I got the scaleFactor value local animGroup = pages[101].animGroup animGroup.anchorChildren = true animGroup.anchorX = 0 animGroup.anchorY = 0 animGroup:scale(illuScale, illuScale) animGroup.x = display.screenOriginX animGroup.y = display.screenOriginY sceneGroup:insert(animGroup) mainGroup = display.newGroup() sceneGroup:insert( mainGroup ) uiGroup = display.newGroup() sceneGroup:insert( uiGroup ) ...
I have been thinking maybe I should put the deviceRatio function in config.lua. However, if doing so, I cannot pass the scaleFactor to the other lua with composer.setVariable() and composer.getVariable.
Any idea why the scaleFactor could be passed in Corona Simulator, but not on build ? What should I do?
Thank you in advance.