Game Development Reference
In-Depth Information
So far, the
GameScene
has relied on the ordering of nodes in SpriteBuilder to determine
the draw order. The level content (
_levelNode
) is drawn first, and then the
GameMenuLayer
is drawn on top of the level.
However, now you are removing the
_levelNode
, loading a new one, and adding it as a
child node to the
GameScene
. Adding a node will always put it at the end of the list;
therefore, the draw order is now reversed, and you can no longer see the pause button.
Listing 8-15
.
Fixing the draw order
level.zOrder = -1;
[self addChild:level];
The
zOrder
property determines the node's draw order. Nodes with a lower
zOrder
are
drawn before nodes with a higher
zOrder
, but only if they have the same parent. Nodes
with the same
zOrder
are drawn in the order they were added to the parent. Note that the
preceding code can be shortened to a single line:
[self addChild:level z:-1];
Now loading levels works! But you want proof? Okay, there's one thing to consider, any-
way: right now the
GameScene
references the
Level1.ccb
in the
GameScene.ccb
file. So
when you load the
GameScene
, it will actually load the
Level1.ccb
along with it, and
then replace it with a newly loaded instance of whatever level you chose to load. That's
kind of inefficient, having two levels loaded one after the other and having both in
memory at the same time.
Adding a Dummy Level
To fix this, go into SpriteBuilder and right-click the
Levels
folder. Select
New File
, and
name it
DummyLevel.ccb
. Set its type to
Node
, and click
Create
. That's all. You just need
an empty CCB.
Now open the
GameScene.ccb
, and select the level
Content
node. On the
Item Properties
tab, click the
CCB File
drop-down menu. Select the newly created
Levels/
DummyLevel.ccb
as the
Sub File
node's reference. The
GameScene
will be empty now
except for the pause button in the upper left corner.
Search WWH ::
Custom Search