Dear corona community,
I have been struggling with this for months now and I just dont understand whats going on here. Here is what I’d like to do simplified:
I have three Composer scenes:
Scene A just shows a “welcome” screen, nothing special here
Scene B has two display.newContainers on it. They show images I set in Scene C, or, if no images have been taken yet, just a “tap to add image” text.
Scene C just shows a “+” icon to start the camera. The picture that is taken is stored in a modul “globals.lua”
So, if everything goes well, the app is started, user goes from Scene A to B, tapps on the first container to move on to Scene C and takes a picture. Since this picture is stored in globals.lua, once he goes back to scene B, the first container now shows this picture. The user then can tap on the second container, again is moved to scene C, where he takes the second picture. When he goes back to scene B, he now sees both pictures.
Everything works perfectly in the simulator, but as soon as I upload it on my android smartphone, the following happens:
I can perfectly set the first picture. But when I set the second picture and go back to scene B that is now supposed to show both pictures, the first picture got removed.
So, why does it work in the simulator and not on my device?
Scene A “passPic1” shouldnt be relevant, but here is the scene:create part:
function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local buttonScene1, buttonScene2, buttonScene3 local gotoScene1, gotoScene2, gotoScene3 local welcomeText -- listener functions for scene change function gotoScene1() composer.gotoScene("passPic1") end function gotoScene2() composer.gotoScene("passPic2") end function gotoScene3() composer.gotoScene("passPic3") end -- add button1 buttonScene1 = display.newImageRect( "button1.jpg", 30,30 ) buttonScene1.x = display.contentCenterX - 50 buttonScene1.y = display.viewableContentHeight - 50 buttonScene1:addEventListener( "tap", gotoScene1 ) -- add button2 buttonScene2 = display.newImageRect( "button2.jpg", 30,30 ) buttonScene2.x = display.contentCenterX buttonScene2.y = display.viewableContentHeight - 50 buttonScene2:addEventListener( "tap", gotoScene2 ) -- add button3 buttonScene3 = display.newImageRect( "button3.jpg", 30,30 ) buttonScene3.x = display.contentCenterX + 50 buttonScene3.y = display.viewableContentHeight - 50 buttonScene3:addEventListener( "tap", gotoScene3 ) -- welcome text welcomeText = display.newText(sceneGroup, "welcome", display.contentWidth/2, display.contentCenterY, native.systemFontBold, 22 ) welcomeText:setFillColor( 0 ) end
Scene B “passPic2” where the two containers check if pictures have been taken:
local composer = require( "composer" ) local globalSpace = require("globals") local scene = composer.newScene() local topTemplate, bottomTemplate local topImage, bottomImage local choosePic1, choosePic2 -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- 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 -- add top image template container topTemplate = display.newContainer(100,100) topTemplate.x = display.contentWidth/2 topTemplate.y = display.viewableContentHeight/2 - 55 sceneGroup:insert(topTemplate) -- add bottom image template container bottomTemplate = display.newContainer(100,100) bottomTemplate.x = display.contentWidth/2 bottomTemplate.y = display.viewableContentHeight/2 + 55 sceneGroup:insert(bottomTemplate) -- listener functions to add images function choosePic1 ( event ) local options = { params = {img = 1} } composer.gotoScene("passPic3", options) end function choosePic2 ( event ) local options = { params = {img = 2} } composer.gotoScene("passPic3", options) end -- add listeners to add image topTemplate:addEventListener( "tap", choosePic1 ) bottomTemplate:addEventListener( "tap", choosePic2 ) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) -- fill top image container topImage = globalSpace.getImg1() if(topImage) then topImage = globalSpace.getImg1() topTemplate:insert(topImage) topImage.x = 0 topImage.y = 0 else topImage = display.newImageRect(topTemplate, "addImage.jpg", 100,100 ) end -- fill bottom image container bottomImage = globalSpace.getImg2() if(bottomImage) then bottomImage = globalSpace.getImg2() bottomTemplate:insert(bottomImage) bottomImage.x = 0 bottomImage.y = 0 else bottomImage = display.newImageRect(bottomTemplate, "addImage.jpg", 100,100 ) end elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
and finally Scene C “passPic” where the camera action takes place:
local composer = require( "composer" ) local globalSpace = require("globals") local photo local cameraButton local params local imageBounds local finalImage local addImageFromCamera, cameraListener local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- 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 -- function to add picture from camera function addImageFromCamera() if media.hasSource( media.Camera ) then -- take picture media.capturePhoto( { listener=cameraListener } ) else -- load default image photo = display.newImage("default.jpg") photo.width = 300 photo.height = 300 photo.x = display.contentCenterX photo.y = display.contentCenterY sceneGroup:insert(photo) -- set bounds to capture part of image imageBounds = { xMin = display.contentCenterX - 50, xMax = display.contentCenterX + 50, yMin = display.contentCenterY - 50, yMax = display.contentCenterY + 50 } -- capture part of image finalImage = display.captureBounds( imageBounds, false ) finalImage.x = display.contentCenterX finalImage.y = display.contentCenterY sceneGroup:insert(finalImage) photo:removeSelf() photo = nil if(option == 1) then globalSpace.setImg1(finalImage) else globalSpace.setImg2(finalImage) end end end -- listener function to receive image from camera function cameraListener(event) photo = event.target photo.width = 300 photo.height = 300 photo.x = display.contentCenterX photo.y = display.contentCenterY sceneGroup:insert(photo) -- set bounds to capture part of image imageBounds = { xMin = display.contentCenterX - 50, xMax = display.contentCenterX + 50, yMin = display.contentCenterY - 50, yMax = display.contentCenterY + 50 } -- capture part of image finalImage = display.captureBounds( imageBounds, false ) finalImage.x = display.contentCenterX finalImage.y = display.contentCenterY sceneGroup:insert(finalImage) photo:removeSelf() photo = nil if(option == 1) then globalSpace.setImg1(finalImage) else globalSpace.setImg2(finalImage) end end -- button to take picture with camera (or take default if no camera) cameraButton = display.newImage(sceneGroup, "buttonAdd.png") cameraButton.x = display.contentCenterX cameraButton.y = 20 cameraButton:addEventListener( "tap", addImageFromCamera ) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) -- get scene parameters to decide whether image1 or image2 is set local params = event.params local options if(params) then option = params.img else option = 1 end elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
here is the globals.lua:
local globalSpace = {} local img1, img2 local function setImg1(img) img1 = img end local function setImg2(img) img2 = img end local function getterImg1() return img1 end local function getterImg2() return img2 end globalSpace.setImg1 = setImg1 globalSpace.setImg2 = setImg2 globalSpace.getImg1 = getterImg1 globalSpace.getImg2 = getterImg2 return globalSpace
I would be more than happy if anyone could have a look at this. Any ideas and suggestions are highly appreciated.
Thanks in advance
