Game Development Reference
In-Depth Information
Time for action - creating
SpriteBatchNode
Let's begin implementing the
createGameScreen
method in
GameLayer.cpp
. Just
below the lines that add the
bg
sprite, we instantiate our batch node:
void GameLayer::createGameScreen() {
//add bg
auto bg = Sprite::create("bg.png");
...
SpriteFrameCache::getInstance()->
addSpriteFramesWithFile("sprite_sheet.plist");
_gameBatchNode =
SpriteBatchNode::create("sprite_sheet.png");
this->addChild(_gameBatchNode);
In order to create the batch node from a sprite sheet, we first load all the frame information
described by the
sprite_sheet.plist
file into
SpriteFrameCache
. And then we
create the batch node with the
sprite_sheet.png
file, which is the source texture
shared by all sprites added to this batch node. (The background image is not part of the
sprite sheet, so it's added separately before we add
_gameBatchNode
to GameLayer.)
Now we can start putting stuff inside
_gameBatchNode
.
1. First, the city:
for (int i = 0; i < 2; i++) {
auto sprite = Sprite::createWithSpriteFrameName
("city_dark.png");
sprite->setAnchorPoint(Vec2(0.5,0));
sprite->setPosition(_screenSize.width * (0.25f + i
* 0.5f),0));
_gameBatchNode->addChild(sprite, kMiddleground);
sprite =