Storyboard/Composer

To my understanding at least when using storyboard/composer you have to spawn all objects within a function and add them to a self.view group. I thought that if you create a variable within a local function it is not in scope of any other chunk of code apart from that function. I am a noob so forgive me if I asked a silly question, but am i correct ?

You can always create a forward reference at the top of your code (outside of any functions) and then it’s available to all functions inside that scene. Quick example:

local enemyShip local function createEnemy() enemyShip = display.newImage("enemyship.png") end createEnemy()

The variable enemyShip is a local variable, but it’s “global” to all the functions in that scene.

You could also create a display object and pass back the object, so it would look something like this:

local function createEnemy() local foo = display.newImage("enemyship.png") return foo end local enemyShip = createEnemy()

In that case the foo variable is not seen outside of that function, but enemyShip is now available to all code in the function where it was created.

I hope that helps.

 Jay

[EDIT: I didn’t insert either of those into the scene.view group but that doesn’t change how the variables are seen, anyway.]

What Jay is describing is called Upvalues in Lua terms.  You are limited to 60 of these per module, and I frequently do this if I need to access things in multiple functions.  Objects like your background, somethings like buttons with touch handlers don’t need to be referenced outside of the scene they are created in.  The reference to the buttons is passed in to the event handler as event.target, etc. 

If you need a function to spawn enemies, coins, etc. That function can live outside of the scene:create() function and just have scene:create() call it when needed and store the objects in a forward declared table.

And one other thing you can do, which might make more sense is to add these object’s to the scene table.  When you start a scene, you do something like:

local scene = composer.newScene()

This creates an object called scene that the create, show, etc. functions are attached.  Inside of those functions, “self” references the scene itself, so you can do:

self.myObject = display.newImage(“myimage.png”)

Outside of the scene:functions, you can reference it as scene.myObject, inside those functions, you can do self.myObject.

Rob

You can always create a forward reference at the top of your code (outside of any functions) and then it’s available to all functions inside that scene. Quick example:

local enemyShip local function createEnemy() enemyShip = display.newImage("enemyship.png") end createEnemy()

The variable enemyShip is a local variable, but it’s “global” to all the functions in that scene.

You could also create a display object and pass back the object, so it would look something like this:

local function createEnemy() local foo = display.newImage("enemyship.png") return foo end local enemyShip = createEnemy()

In that case the foo variable is not seen outside of that function, but enemyShip is now available to all code in the function where it was created.

I hope that helps.

 Jay

[EDIT: I didn’t insert either of those into the scene.view group but that doesn’t change how the variables are seen, anyway.]

What Jay is describing is called Upvalues in Lua terms.  You are limited to 60 of these per module, and I frequently do this if I need to access things in multiple functions.  Objects like your background, somethings like buttons with touch handlers don’t need to be referenced outside of the scene they are created in.  The reference to the buttons is passed in to the event handler as event.target, etc. 

If you need a function to spawn enemies, coins, etc. That function can live outside of the scene:create() function and just have scene:create() call it when needed and store the objects in a forward declared table.

And one other thing you can do, which might make more sense is to add these object’s to the scene table.  When you start a scene, you do something like:

local scene = composer.newScene()

This creates an object called scene that the create, show, etc. functions are attached.  Inside of those functions, “self” references the scene itself, so you can do:

self.myObject = display.newImage(“myimage.png”)

Outside of the scene:functions, you can reference it as scene.myObject, inside those functions, you can do self.myObject.

Rob