Changing Screens

I’m trying out the screen pages with director.

I have a level 1 and an about page, each have a back button on them to go back to the menus.

Here is my about page.

[code]
module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

– Above is the standard stuff, commented in MENU.LUA


– SCENE SETUP –

local background = display.newImage (“images/bluescreen.png”)
localGroup:insert(background)
– Sets the background
local aboutText = display.newText("", 0, 0, native.systemFontBold, 17)
aboutText:setReferencePoint(display.CenterReferencePoint)
aboutText.x = display.contentWidth * 0.5
aboutText.y = display.contentHeight * 0.5 - 40
aboutText:setTextColor(238, 238, 238) – #EEEEEE
aboutText.text = “War Dogs is about\n Dogs of War”

local aboutText2 = display.newText("", 0, 0, native.systemFontBold, 13)
aboutText2:setReferencePoint(display.CenterReferencePoint)
aboutText2.x = display.contentWidth * 0.5
aboutText2.y = aboutText.y + 17
aboutText2:setTextColor(238, 238, 238) – #EEEEEE
aboutText2.text = “For more information and updates visit:”

local link = display.newText("", 0, 0, native.systemFontBold, 12)
link:setReferencePoint(display.CenterReferencePoint)
link.x = display.contentWidth * 0.5
link.y = aboutText2.y + 30
link:setTextColor(229, 175, 6) – #E5AF06
link.text = “http://link.com

–=======================SO Back Button
local backbutton = display.newImage (“images/back.png”)
backbutton.x = display.contentWidth * 0.1
backbutton.y = display.contentHeight * 0.9
localGroup:insert(backbutton)
– Sets the icon for back Button

local function gotoback (event)
director:changeScene(“menu”)
aboutText.text = " "
aboutText2.text = " "
link.text = " "
end
backbutton:addEventListener(“tap”, gotoback)



– Below is the standard stuff, commented in MENU.LUA
– MUST return a display.newGroup()
return localGroup
end[/code]

When I go back the text stays on the screen. I added blank .text= in the function to go back, is that the correct way of doing this?

Also on my level 1 page I have the same back button, but it doesn’t do anything. The debugger screen just says- Loading our data…
[import]uid: 78446 topic_id: 12956 reply_id: 312956[/import]

You are not inserting the text bits into localGroup, when you return, localGroup is removed, but the text objects are not part of the group, so they remain.
[import]uid: 19626 topic_id: 12956 reply_id: 47565[/import]

I thought everything is in the local group per line 5.

Or do I understand this wrong?

Dan [import]uid: 78446 topic_id: 12956 reply_id: 47584[/import]

All line 5 does is to create the localGroup. You still have to do a localGroup:insert() on any display object that you want Director to remove when switching screens.

Think of a display.newGroup() (in this case “localGroup”) as a big bucket whose job is to hold other display objects. Things don’t get into the bucket until you put them in it. Then when you return to your menu level, Director then dumps all of the stuff out of the bucket for you. The bucket itself doesn’t do much else.

[import]uid: 19626 topic_id: 12956 reply_id: 47610[/import]

If you use Direction Class v1.3, Everything should add to local Group exam: text, Image
you can use localGroup:insert() as robmiracle recommend.
When system change to other screen, It will be remove every object in localGroup as automatically.

Mate [import]uid: 58733 topic_id: 12956 reply_id: 47617[/import]

Ok, I got it to work.

Now on my game page with a lot of objects, so I have to add an insert to each one?

Also I placed the game pause, resume, display etc. and call it this way

local gamecontrols = require( "gamecontrols" )  
local gamecontrols = gamecontrols.gamecontrols  
gamecontrols()  

do I insert like this-

local gamecontrols = require( "gamecontrols" )  
local gamecontrols = gamecontrols.gamecontrols  
gamecontrols()  
localGroup:insert(gamecontrols)  

I tried each object in the gamecontrols page and they didn’t load. [import]uid: 78446 topic_id: 12956 reply_id: 47619[/import]

Your gamecontrols.lua file should create a display group inside itself:

[lua]function gamecontrols()
local g = display.newGroup()

b = display.newImage(“button.png”);
g:insert(b)
p = display.newImage(“player.png”);
g:insert§

return g
end[/lua]

Then when you call this routine:

[lua]local gamecontrols = require( “gamecontrols” )
local gamecontrols = gamecontrols.gamecontrols
– the call below will do the work in the gamecontrol function, but noting is returned for us to use.
– so this line needs changed from:
– gamecontrols()
– to:
local myGameControlGroup = gamecontrols()

localGroup:insert(myGameControlGroup)[/lua]

Now, we are taking the display group created in the function gamecontrols() and putting that group into our localGroup.

[import]uid: 19626 topic_id: 12956 reply_id: 47693[/import]

That’s great. Thanks.

Dan [import]uid: 78446 topic_id: 12956 reply_id: 47799[/import]

I have another one-

5 icons that appear as health

As this, the icons appear correct, but get
bad argument #-2 to ‘insert’ in function insert -
<br> local lifeIcons = {}<br> local lives = 5<br> local maxLives = 5<br> local i <br> <br> for i = 1, maxLives do <br> lifeIcons[i] = display.newImage("images/lifeicon.png")<br> lifeIcons[i].x = 10 + (lifeIcons[i].contentWidth * (i - 1))<br> lifeIcons[i].y = 90 <br> end<br> localGroup:insert(lifeIcons)<br>

if I try this the icons appear correct, I get an ERROR: table expected
<br>localGroup:insert(lifeIcons[i])<br>

Lastly I try this, I get no errors and everthing works except I only get 1 icon

<br>local lifeIcons = {}<br> local lives = 5<br> local maxLives = 5<br> local i <br> boneicons = display.newImage("images/lifeicon.png")<br> <br> for i = 1, maxLives do <br> lifeIcons[i] = boneicons<br> lifeIcons[i].x = 10 + (lifeIcons[i].contentWidth * (i - 1))<br> lifeIcons[i].y = 90 <br> end<br> localGroup:insert(boneicons) <br>

Also the first one works if I don’t have the localGroup:insert(lifeIcons), but then it always stays visible.

I’m stuck,

Thanks
Dan [import]uid: 78446 topic_id: 12956 reply_id: 47818[/import]

i havent got director to work trying to do images like this either but i was thinking today maybe if you were to remove local as in your code remove local from line 1 not sure if it will work yet im testing something new at the moment
also move the localGroup:insert() to inside the for loop and add the [i] to it
[blockcode]
lifeIcons = {}
local lives = 5
local maxLives = 5
local i

for i = 1, maxLives do
lifeIcons[i] = display.newImage(“images/lifeicon.png”)
lifeIcons[i].x = 10 + (lifeIcons[i].contentWidth * (i - 1))
lifeIcons[i].y = 90
localGroup:insert(lifeIcons[i])
end

[/blockcode] [import]uid: 7911 topic_id: 12956 reply_id: 47819[/import]

This should work for you:

[lua] local lifeIcons = {}
local lives = 5
local maxLives = 5
local i

for i = 1, maxLives do
lifeIcons[i] = display.newImage(“images/lifeicon.png”)
lifeIcons[i].x = 10 + (lifeIcons[i].contentWidth * (i - 1))
lifeIcons[i].y = 90
localGroup:insert(lifeIcons[i])
end[/lua]

I think it’s important to understand the difference between a display group and display objects, and in particular how Director is using the specific display group “localGroup”
As soon as you create a display object, be it an image, a line, a box, a circle or text, it becomes visible to the player. You don’t have to actually put things in groups to make them work (assuming you’re not using director). In my game OmniBlaster (http://bit.ly/omniblaster) My gameplay screen is completely ungroupd (well the rocket is a group with animation for the flame coming out the back) but all the enemies, rocks, and weapon fire never get put into groups.

What I’ve found the best use for groups are is being able to remove lots of objects at once, say my settings screen. I’ll load a background over top of my game play screen, add a bunch of buttons, text etc. Insert them all into my settings group. When it comes time to dismiss that settings screen, I just remove the group in once call rather than having to remove each item individually. Its also useful if you have a complex object (like my ship which is made up of multiple images) to be able to move them as a whole.

This rapid deletion is why you have to put each display object into “localGroup” when using director. The group is just a logical container of a group of display objects. Director will animate the group then remove it when you hit the back button cleaning up all of your display objects for you.

The reason your code above wasn’t working in the first version was that the loop drew all of your life icons, and that :insert is expecting a display object, not an array of them. Simply move that inside your loop and add a “[i]” so that your referencing the display object at that array position and away you go.

The last version, you only have one object “boneicons” and you’re storing a reference to it in 5 spots in the array. Each of them refer to a single display object. It DOES NOT make a copy of the display object. This burned me too when I was first learning Lua. When the loop is done, your one object will have the last set of positions the loop executed and only one object will show.

I am however puzzled by your description for your second option. That syntax looks right, it just needs moved inside your loop, but that wouldn’t cause that error. [import]uid: 19626 topic_id: 12956 reply_id: 47866[/import]

that code doesn’t work when using director it will work without director
if you were to use one of the vector objects or text it will but not with images
and since he said he had a director error we know he is using it
and although rapid deletion is a great option when using groups there are other good reasons to use groups [import]uid: 7911 topic_id: 12956 reply_id: 47868[/import]

@jstrahan would you care to elaborate? I put image objects in and out of Director based local groups and it manages them just fine in the couple of director based games I have in development.

I’m certain there are lots of great uses, but for me, ease of cleanup in the primary reason. I’d love to hear the different ways you find good uses for, but we shouldn’t hijack this thread for that. [import]uid: 19626 topic_id: 12956 reply_id: 47871[/import]

agreed I post later when I get off work in a new thread [import]uid: 7911 topic_id: 12956 reply_id: 47872[/import]