Director groups - does one need to add each object to it?

Hi,

i wrote a simple single screen game and adapted it within one main.lua file. Now i am re-architecting to use director and other lua utilities.

My question is around director class - does each object need to be added to the maingroup that the core game returns. I have seen various samples do it differently. Even if i leave some objects outside of the group, it still seems to work.

Also, when i use a group, all display objects seems to be defaulting to the centerreferencepoint, which makes it difficult to position naturally, so do developers change the reference point to topleft each time.

i have seen samples using a hard coded coordinate system and some using relative reference using some part of display.contentWidth and display.contentHeight. both seem to work atleast in the simulator.

i will be scanning the sample code once again tonight, but need some good practice pointers from experience developers who have done this before.

thanks in advance.

[import]uid: 55009 topic_id: 11452 reply_id: 311452[/import]

I always set the reference point manually. This way I can use separate reference points as I need them for placing objects.

I also use cached references to contentWidth and contentHeight for placing objects, as well as I will use previously placed objects positions as starting points and add an offset to them. This allows me to place things relative to the screen edges, and other objects, which helps keep a consistent look across different sized devices.

For Director, I make sure that ALL objects that are placed on screen are part of a display group. These display groups are then inserted into the main display group that is returned. This gives me the flexibility to control the display order of the objects, and also insures that they (all the display objects) are cleaned up correctly when switching scenes.

Just make sure to hold references to all transitions, timers and Runtime listeners, so you can nil them out before you switch scenes. [import]uid: 5317 topic_id: 11452 reply_id: 41497[/import]

Why would you want to clean up all objects when switching scenes? If the user goes to the menu in the middle of game play, does the game scene get cleaned up, forcing them to start over?

Or maybe we’re talking about different things when we refer to “scenes”…
[import]uid: 58455 topic_id: 11452 reply_id: 41519[/import]

Switching to a pause screen is a different situation, and one that can be handled using the pop-up functionality in Director 1.3.

Using Director, when you switch scenes, all the display objects are cleaned up. I’m sure there is a way to NOT clean up all the objects, or leave specific objects hanging around, but I haven’t needed to do that. [import]uid: 5317 topic_id: 11452 reply_id: 41528[/import]

Hi,

i do want to treat the top left corner of the screen (portrait) as 0,0 (768*1024 mode)

for some reason my 0,0 is always centered. how do i change it to be top left?

Here is the code in my menu screen: (using director)

[lua]module(…, package.seeall)

local gameGroup = display.newGroup()

require(“director”)

function new()

display.setStatusBar(display.HiddenStatusBar)

local launchScreen = display.newGroup()
launchScreen.x = _w/2
launchScreen.y = _h/2

local backGround = display.newImageRect(“images/background.jpg”,display.contentWidth,display.contentHeight,true)

launchScreen:insert(backGround)

local title = display.newImageRect(“images/title1.png”, 774, 252, true)
title.y = 20
launchScreen:insert(title)

gameGroup:insert(launchScreen)

– clean everything up
clean = function ()

end

– do not remove lest the sky shalt fall upon thine head
return gameGroup

end[/lua] [import]uid: 55009 topic_id: 11452 reply_id: 41654[/import]

You’re setting these coordinates to the middle of the screen, assuming _w and _h are display width and height.

launchScreen.x = \_w/2  
launchScreen.y = \_h/2  

[import]uid: 58455 topic_id: 11452 reply_id: 41680[/import]

To further clarify, the top left corner of the screen is always 0,0

For example:
[lua]_w = display.contentWidth --this equals 768
_h = display.contentHeight – this equals 1024[/lua]

Given that, _w/2 will equal 384, and _h/2 will equal 512.

Where your display objects show up is a combination of setting the reference point of the display object, and where you position it.

If you have an image, whose reference point is CenterReferencePoint, and is placed at 0,0 it will look like the bottom right quadrant of your image is visible (since Corona will place the reference point at the co-ordinates specified). Now if you move the reference point to TopLeftReferencePoint, and placed at 0,0 it will look correct, since the top left corner will be placed at 0,0.

Does that make sense to you? Let me know if you need more help.
[import]uid: 5317 topic_id: 11452 reply_id: 41695[/import]

Thanks mike. That does.

I think where i was grappling was that setReferencePoint did not work as expected. Try it. when we load an image using display.newImageRect(…), the setReferencePoint does not work as expected.

So now i set the referencePoint manually using xReference and yReference,

[lua]setTopLeftReferencePointImages = function(obj)
obj.xReference = -obj.width/2
obj.yReference = -obj.height/2
obj.x = 0
obj.y = 0
end[/lua]

thanks once again [import]uid: 55009 topic_id: 11452 reply_id: 41700[/import]

What do you mean setReferencePoint doesn’t work as expected?

I use this in daily build 540, and it works as expected:
[lua]local rightWall = display.newImageRect( “leftWallDash.png”, 2, 266 );
rightWall:setReferencePoint ( display.CenterLeftReferencePoint );[/lua] [import]uid: 5317 topic_id: 11452 reply_id: 41709[/import]

Oh! well i use .484 and that seems to have this particular issue. thanks [import]uid: 55009 topic_id: 11452 reply_id: 41710[/import]