Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Composer Scenes Not Working in Conjunction With Other Scenes
Started by homelesspete Apr 09 2018 01:30 AM

- - - - -
3 replies to this topic
scenes contentbounds composer

Best Answer Rob Miracle , 09 April 2018 - 05:51 AM

Hello @homelesspete and welcome to the Corona forums.

 

As a rule, main.lua should never be thought of as a scene. It's a launch point. You can't to a gotoScene() to get back to main.lua unless you jump through a bunch of hoops.  I wouldn't even create any display objects in main.lua, just use it to set things up and call your scenes.

 

It seems to me that with our two buttons in main.lua, you want to treat it as your game/app's menu.  I would instead create a menu.lua scene that is a full Composer scene, create your buttons there and simply have main.lua go a composer.gotoScene("menu") as the last line in main.lua. You won't have to be making those buttons global, hiding and showing them yourself. Let composer do that work for you.

 

Then in your AA and BB scenes, when you tap on your button there, have the button handler simply do a composer.gotoScene("menu") to get back to your menu.

 

Finally, you're using a lot of global functions such as:

function ReturnToMenuListener(event)

When you repeat that function definition in BB, it's going to overwrite the one you defined in AA.  Lua is a single pass compiler which means you have to develop the habit of writing functions before you use them the first time. Simply move that function somewhere above your scene:create() function and put the work "local" in front of the function to make them local to that scene.

 

The sooner you can break your global habit, the more successful you will be with Lua and Corona.

 

Rob

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

homelesspete

[GLOBAL: userInfoPane.html]
homelesspete
  • Observer

  • 2 posts
  • Corona SDK

I have two scenes which work perfectly fine by themselves, even when activated multiple times.  However, if I activate one scene and then the other, the program crashes with a "attempt to index field 'contentBounds' (a nil value)" error.

 

From my research thus far, I've concluded this error is being caused by an internal function call of  'composer.gotoScene' via '_saveSceneAndHide'.  ('contentBounds' isn't anywhere in the code itself.)

 

My current hypothesis is that this is happening because I'm not returning to some prior scene; most of the code is running inside main.lua and I've recently started adding scenes for auxiliary stuff (e.g. a Settings Menu).  I didn't know about scenes when I started this project and I've been trying to use the best methods as I move forward.

 

So, I guess I'm asking: am I required to use scenes differently if I'm going to use them (more than 1, anyway), or is there something else I'm doing wrong / another method?

 

 

Here's simplified example code I made to verify the error wasn't from something else.  Going to either scene works fine, but it crashes if you go to one scene and then to the other:

 

main.lua

local composer = require("composer")

ButtonAA = display.newText("AAAAA", 100, 300, native.systemFont, 25);
ButtonBB = display.newText("BBBBB", 500, 300, native.systemFont, 25);

function ButtonAAListener(event)
	composer.gotoScene("sceneAA")
end

function ButtonBBListener(event)
	composer.gotoScene("sceneBB")
end

ButtonAA:addEventListener("tap", ButtonAAListener)
ButtonBB:addEventListener("tap", ButtonBBListener)

sceneAA / sceneBB.lua:

local composer = require("composer")
local scene = composer.newScene()
 
function scene:create(event)
  local sceneGroup = self.view
		
	ButtonAA.alpha = 0
	ButtonBB.alpha = 0
		
	local ReturnToMenuText = display.newText("Return AA", 140, 300, native.systemFont, 30);
	sceneGroup:insert(ReturnToMenuText)
	ReturnToMenuText:addEventListener("tap", ReturnToMenuListener)
end 

function ReturnToMenuListener(event)
	composer.removeScene("sceneAA")
	
	ButtonAA.alpha = 1
	ButtonBB.alpha = 1
end
 
scene:addEventListener("create", scene)
return scene

(AA and BB are identical except for changing the text label and the scene to remove.)



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

  Best Answer

Hello @homelesspete and welcome to the Corona forums.

 

As a rule, main.lua should never be thought of as a scene. It's a launch point. You can't to a gotoScene() to get back to main.lua unless you jump through a bunch of hoops.  I wouldn't even create any display objects in main.lua, just use it to set things up and call your scenes.

 

It seems to me that with our two buttons in main.lua, you want to treat it as your game/app's menu.  I would instead create a menu.lua scene that is a full Composer scene, create your buttons there and simply have main.lua go a composer.gotoScene("menu") as the last line in main.lua. You won't have to be making those buttons global, hiding and showing them yourself. Let composer do that work for you.

 

Then in your AA and BB scenes, when you tap on your button there, have the button handler simply do a composer.gotoScene("menu") to get back to your menu.

 

Finally, you're using a lot of global functions such as:

function ReturnToMenuListener(event)

When you repeat that function definition in BB, it's going to overwrite the one you defined in AA.  Lua is a single pass compiler which means you have to develop the habit of writing functions before you use them the first time. Simply move that function somewhere above your scene:create() function and put the work "local" in front of the function to make them local to that scene.

 

The sooner you can break your global habit, the more successful you will be with Lua and Corona.

 

Rob



[TOPIC: post.html]
#3

homelesspete

[GLOBAL: userInfoPane.html]
homelesspete
  • Observer

  • 2 posts
  • Corona SDK

Not the answer I wanted, though the one I expected I guess =)

 

The ordered function thing was one of the worst thing about learning Lua for me, but fortunately forward-declarations mostly prevent that from being frustrating.  In the code proper, most variables and all functions are locals (though I suppose local functions don't matter much when they're all in one file :P)

 

 

I suppose that means I need to figure out how interested I am in splitting 1000 lines of code into ~7 separate scenes with some functions shared between them, oi.



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

Lua has a limit of 200 local variables in a module, so you will either eventually start grouping variables and functions in tables anyway.

 

Lua modules are super easy to use. You create a new .lua file, add a local table to it, add any variables or functions to the table and return the table:

-- mymodule.lua

local myModule = {}
myModule.score = 0
function myModule.addToScore( self, value )
     self.score = self.score + value
end

return myModule

Then when you need it:

local myModule = require("mymodule")

It's much easier to scroll up and down a 20-30 line module than a 1000 line main program.

 

Rob




[topic_controls]
[/topic_controls]