So I’m transferring a game over to Composer. After reading the Docs and tutorials on composer I noticed that Display images need to go inside the scene:create area of the composer template. It just so happens that I have a function that displays Images so that I can refresh an inventory HUD. From the Docs on Composer, functions go before the Scene:create. Now what do I do? Do I rewrite the code or is there a way around this? Thanks in advance.
@animationarsenal, while I’m not sure if you’re going for a game or business app, taking a look at Rob Miracle’s Composer template should provide some insight:
https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/
Thanks for the reply Alex. I have read that tutorial but it doesn’t tell me anything about being able to put display objects in a function that sits outside the scene:create area. Or how I would go about reworking my problem. Thanks again for the quick response.
Putting objects in a function outside of the scene: calls is just fine. if you do something like:
local function callBlock(a,b) local blockHere = display.newRect(0,0,30,30) blockHere.x, blockHere.y = a,b blockHere.id = "I'm a block" return blockHere end --sometime later function scene:show( event ) if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then local blockTest = callBlock(display.contentCenterX, display.contentCenterY) print("blockTest's ID is "..tostring(blockTest.id)) end end
you should see that block just fine. Is this what you were talking about, or did you have a different implementation in mind?
Thanks again Alex for taking the time to respond. i do appreciate it. I think what you have going might be the way I need to go. Here’s the code for the function that sits outside scene:show.
local function hudScreen () if a\>=1 then audio.play (pickUpSound ) end hud = display.newImage("inventoryHUD.png") hud.x=centerX hud.y=742 hud.name="hud" item1=display.newImage(inven[1]..".png") item1.x= 205 item1.y= 742 item1.name="Item1" if inven[1] ~= "nothing" then item1:addEventListener("tap",useStuff) end item2=display.newImage(inven[2]..".png") item2.x=325 item2.y=742 item2.name="Item2" if inven[2] ~= "nothing" then item2:addEventListener("tap",useStuff) end item3=display.newImage(inven[3]..".png") item3.x=450 item3.y=742 item3.name="Item3" if inven[3] ~= "nothing" then item3:addEventListener("tap",useStuff) end item4=display.newImage(inven[4]..".png") item4.x=575 item4.y=742 item4.name="Item4" if inven[4] ~= "nothing" then item4:addEventListener("tap",useStuff) end end
I have added all the images to the scene:group in the composer file even though they are left out in this code because this was the function that I was trying to transfer over.
I don’t know what the rest of your code looks like so I assume this is scoped properly, but I think this might work OK. Are you running into problems with the above?
Yep. I forward referenced all my variables so I can use them in any of the phases so I think my variable scope is fine. For some reason it won’t work. But I might test your method and see if I can accomplish the same thing in a different format and maybe that will work. Thanks Alex for the help. If you say it should work then maybe there’s something I’m not catching in the code.
One small suggestion is to make all of your asset names lowercase. Not that this should impact this problem, but you’ll probably run into fewer issues in the future if you make all names lowercase.
OK. Thanks. I think I was running into some problems because of that. Thanks for the tip. I will be using that. So you’re saying my “inventoryHUD.png” should be “inventoryhud.png” and the UseStuff function should just be usestuff. Right?
actual Lua syntax doesn’t present a problem with cases, but it’s usually best to use lowercase for assets.
Ok I will keep that in mind when I write the rest of the code. Alex, thanks for your time. You’ve been a great help.
Composer and Storyboard are very alike in how you deal with display objects. If you do not put a created display object in the scene’s view group, it will sit on top of the scene and not be managed by Composer (or Storyboard). If you put the object in the group, then it will be managed by the scene manager.
The scene object’s view group (nothing more than a display.newGroup() for all practical purposes) is referenced as:
scene.view
This means that any display object can simply be done via:
scene.view:insert( object )
anywhere in the scene. Both Composer and Storyboard support some advanced features where thinking about scenes as objects is helpful, so when the events fire that call scene:create(), scene:show() etc., the scene object is passed to those event functions as “self” The “:” operator between the object and the function name causes “self” to be the first parameter, which is the object itself and hidden from the parameter list. This is why in those functions we do:
local sceneGroup = self.view
because self.view is scene.view. If you want the object to be managed, put it in the scene’s group, else it will just sit on top.
Rob
Thanks Rob for the response. The function that I posted above was before I took it into composer so when I transferred it over I did add the display objects to the scene group. Thanks again for the information Rob it’s very useful to know and I always appreciate your comments.
@animationarsenal, while I’m not sure if you’re going for a game or business app, taking a look at Rob Miracle’s Composer template should provide some insight:
https://coronalabs.com/blog/2015/04/14/tutorial-the-basic-game-template/
Thanks for the reply Alex. I have read that tutorial but it doesn’t tell me anything about being able to put display objects in a function that sits outside the scene:create area. Or how I would go about reworking my problem. Thanks again for the quick response.
Putting objects in a function outside of the scene: calls is just fine. if you do something like:
local function callBlock(a,b) local blockHere = display.newRect(0,0,30,30) blockHere.x, blockHere.y = a,b blockHere.id = "I'm a block" return blockHere end --sometime later function scene:show( event ) if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then local blockTest = callBlock(display.contentCenterX, display.contentCenterY) print("blockTest's ID is "..tostring(blockTest.id)) end end
you should see that block just fine. Is this what you were talking about, or did you have a different implementation in mind?
Thanks again Alex for taking the time to respond. i do appreciate it. I think what you have going might be the way I need to go. Here’s the code for the function that sits outside scene:show.
local function hudScreen () if a\>=1 then audio.play (pickUpSound ) end hud = display.newImage("inventoryHUD.png") hud.x=centerX hud.y=742 hud.name="hud" item1=display.newImage(inven[1]..".png") item1.x= 205 item1.y= 742 item1.name="Item1" if inven[1] ~= "nothing" then item1:addEventListener("tap",useStuff) end item2=display.newImage(inven[2]..".png") item2.x=325 item2.y=742 item2.name="Item2" if inven[2] ~= "nothing" then item2:addEventListener("tap",useStuff) end item3=display.newImage(inven[3]..".png") item3.x=450 item3.y=742 item3.name="Item3" if inven[3] ~= "nothing" then item3:addEventListener("tap",useStuff) end item4=display.newImage(inven[4]..".png") item4.x=575 item4.y=742 item4.name="Item4" if inven[4] ~= "nothing" then item4:addEventListener("tap",useStuff) end end
I have added all the images to the scene:group in the composer file even though they are left out in this code because this was the function that I was trying to transfer over.
I don’t know what the rest of your code looks like so I assume this is scoped properly, but I think this might work OK. Are you running into problems with the above?