Game Development Reference
In-Depth Information
Time for action - creating placeholder
sprites
So let me show you how to do that:
1. Go ahead and download the
4198_06_START_PROJECT.zip
file if you
haven't done so already.
2. When you open the project in Xcode, you will see all the classes we'll need for the
game, and we'll go over them in a second. But for now, just go to
GameLay-
er.cpp
.
3. Scroll down to the last
createGameScreen
method and add the following
lines:
auto quickSprite = Sprite::create("blank.png");
quickSprite->setTextureRect(Rect(0, 0, 100, 100));
quickSprite->setColor(Color3B(255,255,255));
quickSprite->setPosition(Vec2(_screenSize.width * 0.5,
_screenSize.height * 0.5));
this->addChild(quickSprite);
And that's it. The sprite is created with a texture called
blank.png
. This is a 1 x
1 pixel white square you will find in the
Resources
folder. Then we set the size
of the sprite's texture rectangle to 100 x 100 pixels (
setTextureRect
), and fill
it with a white color (
setColor
). By resizing the texture rectangle, we in effect
resize the sprite. If you run the game now, you should see a white square smack in
the middle of the screen.
4. Now delete the previous lines and replace them with these:
_gameBatchNode = SpriteBatchNode::create("blank.png",
200);
this->addChild(_gameBatchNode, kMiddleground);
This creates
_gameBatchNode
that uses as its source texture the same
blank.png
file. Now we are ready to place as many rectangles inside
_gameBatchNode
as we'd like, and set a different color for each one of them if