Require in a composer scene

Hey I’m new here at Carona SDK and I have a composer issue. I created a visual joystick to control the hero in my game. I have got this joystick in a separate lua  file. When I require this file in my composer scene, it works very well. But, when I go to the start-scene, composer shows my joystick as well.  What is the best way to keep my joystick-file without having this issue? Thank you!  :smiley:

Composer only manages display objects that are in the scene’s “view” (a display group). Every object that you want composer to show and hide as scenes transition and manage their lifecycle have to be inserted into that group. This is why in scene:create() and scene:show() you see the code:

local sceneGroup = self.view

Inside those functions, self refers to the scene and you’re making a local reference to the group.  For functions that are not part of the object (like scene:show(), you can reference the group as scene.view.

Its most likely that your joystick code is just creating the display objects but since they don’t know anything about scenes, they are leaving it there. If your joystick module returns a display.newGroup() or it’s part of the module that’s returned. You can insert that group into the sceneGroup and accomplish what you want.

The other thing to do is realize that Composer isn’t going to manage your joystick and in scene:hide() you can hide/destroy it yourself.

Rob

Hey Rob,

thank you for your fast answer and helping me!

My joystick has two parts. The first part was a success with returning to a display.newGroup():

Joystick file \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... createDpad = function(group)...pad = display.newImageRect( group,"pad.png", pad\_w, pady\_h )... end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Scene \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... function scene:create( event )     local sceneGroup = self.view ... local group = scene.view   pad = display.newGroup()  group:insert(pad)   createDpad(pad) ... end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

That works well and I don’t see it anymore in other scenes. But I have still an issue with the second part my button.

Joystick file \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... createButtonB = function(group) -- Create the widget local buttonb = widget.newButton(     {         label = "button",         onEvent = handleButtonEvent,         emboss = false,         -- Properties for a rounded rectangle button         shape = "roundedRect",         width = 100,         height = 60,         cornerRadius = 5,         fillColor = { default={f,f,f,0.05}, over={f,f,f,0.1} },         strokeColor = { default={0,0,0,0.05}, over={0,0,0,0.1} },         strokeWidth = 4,         labelColor = { default={ f, f, f,0.1 }, over={ f, f, f,0.2 } },         fontSize = 25     } ) buttonb.x = width-60 buttonb.y = height-40 buttonb:setLabel( "B" ) end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Scene \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... function scene:create( event )     local sceneGroup = self.view  local group = scene.view   buttonb = display.newGroup()  group:insert(buttonb)   createButtonB(buttonb) end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

This button is still to see in all scenes. What am I doing wrong? Thank you! :slight_smile:

Inside of createButtonB() you never add the widget.newButton to the group.

Rob

Composer only manages display objects that are in the scene’s “view” (a display group). Every object that you want composer to show and hide as scenes transition and manage their lifecycle have to be inserted into that group. This is why in scene:create() and scene:show() you see the code:

local sceneGroup = self.view

Inside those functions, self refers to the scene and you’re making a local reference to the group.  For functions that are not part of the object (like scene:show(), you can reference the group as scene.view.

Its most likely that your joystick code is just creating the display objects but since they don’t know anything about scenes, they are leaving it there. If your joystick module returns a display.newGroup() or it’s part of the module that’s returned. You can insert that group into the sceneGroup and accomplish what you want.

The other thing to do is realize that Composer isn’t going to manage your joystick and in scene:hide() you can hide/destroy it yourself.

Rob

Hey Rob,

thank you for your fast answer and helping me!

My joystick has two parts. The first part was a success with returning to a display.newGroup():

Joystick file \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... createDpad = function(group)...pad = display.newImageRect( group,"pad.png", pad\_w, pady\_h )... end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Scene \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... function scene:create( event )     local sceneGroup = self.view ... local group = scene.view   pad = display.newGroup()  group:insert(pad)   createDpad(pad) ... end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

That works well and I don’t see it anymore in other scenes. But I have still an issue with the second part my button.

Joystick file \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... createButtonB = function(group) -- Create the widget local buttonb = widget.newButton(     {         label = "button",         onEvent = handleButtonEvent,         emboss = false,         -- Properties for a rounded rectangle button         shape = "roundedRect",         width = 100,         height = 60,         cornerRadius = 5,         fillColor = { default={f,f,f,0.05}, over={f,f,f,0.1} },         strokeColor = { default={0,0,0,0.05}, over={0,0,0,0.1} },         strokeWidth = 4,         labelColor = { default={ f, f, f,0.1 }, over={ f, f, f,0.2 } },         fontSize = 25     } ) buttonb.x = width-60 buttonb.y = height-40 buttonb:setLabel( "B" ) end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Scene \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ ... function scene:create( event )     local sceneGroup = self.view  local group = scene.view   buttonb = display.newGroup()  group:insert(buttonb)   createButtonB(buttonb) end ... \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

This button is still to see in all scenes. What am I doing wrong? Thank you! :slight_smile:

Inside of createButtonB() you never add the widget.newButton to the group.

Rob