Cant get something very simple to work

Hello,

I was just trying to make a simple intro for my game. A background that switches from image and then the app goes to the menu.

I got it working with 2 different scripts for both backgrounds. But I am trying to get the intro it into 1 script.

Somehow it never goes to the menu at this line: storyboard.gotoScene( “menu”, 500 )
But it is still running the print commands i put around it!
And the same line of code works if I use only one background.

Anybody knows what the problem is I can not get it figured out.

Here is my code for the Intro:

-----------------------------------------------------------------------------------------  
--  
-- menu.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local background  
  
-- include Background class  
require "Background"  
--------------------------------------------  
  
-- 'onRelease' event listener for playBtn  
  
local function onTimerExpires()  
 -- Change background Image  
 background = Background:changeImage("Graphics/Intros/introRaB.jpg")  
 --startTimer2()  
 return true -- indicates successful timer expire  
end  
  
local function onTimerExpires2()  
 print("lol")  
 -- go to intro2.lua scene  
 storyboard.gotoScene( "menu", 500 )  
 print("went to menu")  
 return true -- indicates successful timer expire  
end  
-----------------------------------------------------------------------------------------  
-- BEGINNING OF YOUR IMPLEMENTATION  
--   
-- NOTE: Code outside of listener functions (below) will only be executed once,  
-- unless storyboard.removeScene() is called.  
--   
-----------------------------------------------------------------------------------------  
  
-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local group = self.view  
  
 -- display a background image  
 background = Background:new(0,0,display.TopLeftReferencePoint,"Graphics/Intros/introLogo.jpg")  
  
 -- all display objects must be inserted into group  
 group:insert( background )  
end  
  
-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 local group = self.view  
  
 -- INSERT code here (e.g. start timers, load audio, start listeners, etc.)  
 startTimer()  
 startTimer2()  
  
end  
  
-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
 local group = self.view  
  
 -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)  
  
  
end  
  
-- If scene's view is removed, scene:destroyScene() will be called just prior to:  
function scene:destroyScene( event )  
 local group = self.view  
  
end  
  
function startTimer()  
 timer.performWithDelay(5000, onTimerExpires)  
end  
  
function startTimer2()  
 timer.performWithDelay(10000, onTimerExpires2)  
end  
  
-----------------------------------------------------------------------------------------  
-- END OF YOUR IMPLEMENTATION  
-----------------------------------------------------------------------------------------  
  
-- "createScene" event is dispatched if scene's view does not exist  
scene:addEventListener( "createScene", scene )  
  
-- "enterScene" event is dispatched whenever scene transition has finished  
scene:addEventListener( "enterScene", scene )  
  
-- "exitScene" event is dispatched whenever before next scene's transition begins  
scene:addEventListener( "exitScene", scene )  
  
-- "destroyScene" event is dispatched before view is unloaded, which can be  
-- automatically unloaded in low memory situations, or explicitly via a call to  
-- storyboard.purgeScene() or storyboard.removeScene().  
scene:addEventListener( "destroyScene", scene )  
  
-----------------------------------------------------------------------------------------  
  
return scene  

My code for the Background:

[code]
Background = {}

local background

function Background:new(startX, startY, refPoint, imageSource)
background = display.newImage( imageSource )
background:setReferencePoint(refPoint)
background.x = startX
background.y = startY
return background
end

function Background:changeImage(imageSource)
background = display.newImage( imageSource )
end
return Background
[/code] [import]uid: 118839 topic_id: 24399 reply_id: 324399[/import]

Out of interest if in menu.lua you put a print statement at the start, does it print?

I’m wondering if the background just isn’t being removed but the scene is indeed still changing. [import]uid: 52491 topic_id: 24399 reply_id: 98709[/import]

I just tried it, and the print in menu.lua does print!

So I think the background somehow does not get removed. But it does get removed when I would not change the background. So do you have any idea whats going wrong there? Cause its all happening at the same variable (background) which is linked to the group. So my guess would be when it destroys it without changing the background, it should also destroy it when I only change the imagesource…

Thanks in advance [import]uid: 118839 topic_id: 24399 reply_id: 98829[/import]

I tried background:removeSelf() and group:removeSelf()

But both give a runtime error and say it is a nil value [import]uid: 118839 topic_id: 24399 reply_id: 98832[/import]

I think remove self would be the best way to go.

Try something like this:

function Background:changeImage(imageSource)  
 if background then  
 background:removeSelf();  
 background = nil;  
 end  
 background = display.newImage( imageSource )   
end  

(I haven’t tested it so I’m not sure it would work with the rest of your code)

Edit:
The reason you get the runtime error is most likely because you are trying to use removeSelf() on background, when it is already set to nil or not assigned the image yet.
The “if background then” statement assures that you only try to remove the image when it is already created and assigned to ‘background’. [import]uid: 33608 topic_id: 24399 reply_id: 98855[/import]

I had a similar issue and for some reason adding a transition to the scene change fixed my problem. So rather than storyboard.gotoScene( “menu” ) I used storyboard.gotoScene( “menu”, “slideLeft”, 500 ) and it worked. Maybe you have the same problem. [import]uid: 40711 topic_id: 24399 reply_id: 98867[/import]

Thanks for the reply’s i’m going to see if i can get it to work. If it works I will confirm how i did it in this topic. [import]uid: 118839 topic_id: 24399 reply_id: 99184[/import]

I managed to fix the problem!
I changed some things in the background.lua. I made a remove function which is called when the app is going from intro.lua to menu.lua.

Here is my new background.lua code

Background = {}  
  
local background  
  
function Background:new(startX, startY, refPoint, imageSource)   
 background = display.newImage( imageSource )  
 background:setReferencePoint(refPoint)  
 background.x = startX  
 background.y = startY  
 return background  
end  
  
function Background:changeImage(startX, startY, refPoint, imageSource)  
 background = display.newImage( imageSource )   
 background:setReferencePoint(refPoint)  
 background.x = startX  
 background.y = startY  
 return background  
end  
  
function Background:remove()  
-- if background then  
 background:removeSelf()  
 background = nil  
 --end  
 return background  
end  
  
return Background  

And here is my new intro.lua code

-----------------------------------------------------------------------------------------  
--  
-- menu.lua  
--  
-----------------------------------------------------------------------------------------  
  
local storyboard = require( "storyboard" )  
local scene = storyboard.newScene()  
local background  
  
-- include Background class  
require "Background"  
--------------------------------------------  
  
-- 'onRelease' event listener for playBtn  
  
local function onTimerExpires()  
 -- Change background Image  
 background = Background:changeImage(0,0,display.TopLeftReferencePoint,"src/graphics/intros/introrab.jpg")  
  
 --startTimer2()  
 return true -- indicates successful timer expire  
end  
  
local function onTimerExpires2()  
 print("lol")  
 -- go to intro2.lua scene  
 background = Background:remove()  
 storyboard.gotoScene( "menu", 500 )  
  
 print("background removed")  
 return true -- indicates successful timer expire  
end  
-----------------------------------------------------------------------------------------  
-- BEGINNING OF YOUR IMPLEMENTATION  
--   
-- NOTE: Code outside of listener functions (below) will only be executed once,  
-- unless storyboard.removeScene() is called.  
--   
-----------------------------------------------------------------------------------------  
  
-- Called when the scene's view does not exist:  
function scene:createScene( event )  
 local group = self.view  
  
 -- display a background image  
 background = Background:new(0,0,display.TopLeftReferencePoint,"src/graphics/intros/intrologo.jpg")  
  
 -- all display objects must be inserted into group  
 group:insert( background )  
end  
  
-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 local group = self.view  
  
 -- INSERT code here (e.g. start timers, load audio, start listeners, etc.)  
 startTimer()  
 startTimer2()  
end  
  
-- Called when scene is about to move offscreen:  
function scene:exitScene( event )  
 local group = self.view  
  
 -- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)  
  
end  
  
-- If scene's view is removed, scene:destroyScene() will be called just prior to:  
function scene:destroyScene( event )  
 local group = self.view  
  
end  
  
function startTimer()  
 timer.performWithDelay(5000, onTimerExpires)  
end  
  
function startTimer2()  
 timer.performWithDelay(10000, onTimerExpires2)  
end  
  
-----------------------------------------------------------------------------------------  
-- END OF YOUR IMPLEMENTATION  
-----------------------------------------------------------------------------------------  
  
-- "createScene" event is dispatched if scene's view does not exist  
scene:addEventListener( "createScene", scene )  
  
-- "enterScene" event is dispatched whenever scene transition has finished  
scene:addEventListener( "enterScene", scene )  
  
-- "exitScene" event is dispatched whenever before next scene's transition begins  
scene:addEventListener( "exitScene", scene )  
  
-- "destroyScene" event is dispatched before view is unloaded, which can be  
-- automatically unloaded in low memory situations, or explicitly via a call to  
-- storyboard.purgeScene() or storyboard.removeScene().  
scene:addEventListener( "destroyScene", scene )  
  
-----------------------------------------------------------------------------------------  
  
return scene  

But I just encountered a new problem. In the corona simulator the game is working fine,
But I was testing it on my Galaxy Nexus and I am getting the error “This application has been corrupted” !

I checked with ADB and the following errors occurred: see image
s7.postimage.org/gda5mgywp/Errormessages.jpg

I think it has something to do with the background.lua. But I do not have a clue what exactly could be wrong with it.
[import]uid: 118839 topic_id: 24399 reply_id: 99215[/import]

The error is caused by a missing Background.lua file. Check that the file name is exactly the same. The simulator is file name insensitive but the device is not. Background.lua is not the same as background.lua [import]uid: 7559 topic_id: 24399 reply_id: 99223[/import]

That worked! How could i not see that, thanks alot :slight_smile: [import]uid: 118839 topic_id: 24399 reply_id: 99380[/import]