Game Development Reference
In-Depth Information
Then, the game world's list of drawable objects can be changed so it's a sorted
container that's sorted based on the draw order. This way, during the rendering
step, the sorted container can be linearly traversed to draw the sprites in the correct
order.
Click here to view code image
SortedList spriteList
// When creating a new sprite...
Sprite newSprite = specify image and desired x/y
newSprite.drawOrder = set desired draw order value
// Add to sorted list based on draw order value
spriteList .Add( newSprite.drawOrder , newSprite )
// When it's time to draw...
foreach Sprite s in spriteList
s .Draw()
loop
As we will discuss in Chapter 4 , 3D Graphics ”, the painter's algorithm can also
be utilized in a 3D environment, though there are some distinct drawbacks that
come out of this. But for 2D scenes, the painter's algorithm generally works well.
Some 2D libraries, including cocos2d, allow a scene to be composed of any num-
ber of layers, each of which can have a specific draw order. Thus, a game might
have a background layer, a character layer, and a UI layer (in that order, from back
tofront).Eachlayercanthencontainanynumberofsprites.Inourexample,allthe
sprites in the background layer will automatically be placed behind all the sprites
in the character layer, and both the background and character layers will show up
behind the UI one. Within each individual layer, you can still specify the draw or-
der for a particular sprite, except the draw order is only relative to other sprites in
that same layer.
As for the x- and y-coordinates of the sprite, the location it references depends on
the library. In some cases, it might be the top-left corner of the sprite relative to the
top left of the screen, whereas others might refer to the center of the sprite relative
to the bottom left. The choice is arbitrary and comes down to how a particular de-
veloper felt like implementing the functionality.
Search WWH ::




Custom Search