Composer- Do I need to use global variables?

Hey,

First, I’m creating my display objects in scene:create(event).

The  tutorial says I should change the position in the scene:show function. 

But how can I acces my display objects there?

Example: 

scene.create(…)…

local button1 = display.newImageRect("Data/Main\_Menue/button1.jpg",                                      width,height); sceneGroup:insert(button1);

scene:show(…)

button1.x = CenterW; button1.y = CenterH-CenterH/2;

How should I acces them? Via functions?  :huh:

You don’t need to make them global but you can create the variable outside the scope of both functions, and then populate that variable in create() / edit in show()

Example:

--create the variable outside both functions local button1 function scene.create(...) --notice I don't use the word "local" again here, otherwise the variable would be local to the create function button1 = display.newImageRect("Data/Main\_Menue/button1.jpg", width,height); sceneGroup:insert(button1); end function scene:show(.....) button1.x = CenterW; button1.y = CenterH-CenterH/2; end

As a side note, you can save yourself a line of code by adding the group as the first parameter in your newImageRect function:

button1 = display.newImageRect(sceneGroup, "Data/Main\_Menue/button1.jpg", width,height); --now we don't need sceneGroup:insert(button1)

You don’t need to make them global but you can create the variable outside the scope of both functions, and then populate that variable in create() / edit in show()

Example:

--create the variable outside both functions local button1 function scene.create(...) --notice I don't use the word "local" again here, otherwise the variable would be local to the create function button1 = display.newImageRect("Data/Main\_Menue/button1.jpg", width,height); sceneGroup:insert(button1); end function scene:show(.....) button1.x = CenterW; button1.y = CenterH-CenterH/2; end

As a side note, you can save yourself a line of code by adding the group as the first parameter in your newImageRect function:

button1 = display.newImageRect(sceneGroup, "Data/Main\_Menue/button1.jpg", width,height); --now we don't need sceneGroup:insert(button1)