- Cocos2d-x by Example:Beginner's Guide(Second Edition)
- Roger Engelbert
- 394字
- 2021-07-23 20:00:29
Time for action – implementing init()
Inside init()
, we'll build the game screen, bringing in all the sprites and labels we'll need for the game:
- So right after the
if
statement where we call the superLayer::init
method, we add:_players = Vector<GameSprite*>(2); _player1Score = 0; _player2Score = 0; _screenSize = Director::getInstance()->getWinSize();
- We create the vector where we'll store both players, initialize the score values, and grab the screen size from the singleton, all-knowing
Director
. We'll use the screen size to position all sprites relatively. Next we will create our first sprite. It is created with an image filename, whichFileUtils
will take care of loading from the correct folder:auto court = Sprite::create("court.png"); court->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height * 0.5)); this->addChild(court);
- Get into the habit of positioning sprites with relative values, and not absolute ones, so we can support more screen sizes. And say hello to the
Vec2
type definition used to create points; you'll be seeing it a lot in Cocos2d-x. - We finish by adding the sprite as a child to our
GameLayer
(the court sprite does not need to be aGameSprite
). - Next we will use our spanking new
GameSprite
class, carefully positioning the objects on screen:_player1 = GameSprite::gameSpriteWithFile("mallet.png"); _player1->setPosition(Vec2(_screenSize.width * 0.5, _player1->radius() * 2)); _players.pushBack(_player1); this->addChild(_player1); _player2 = GameSprite::gameSpriteWithFile("mallet.png"); _player2->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height - _player1->radius() * 2)); _players.pushBack(_player2); this->addChild(_player2); _ball = GameSprite::gameSpriteWithFile("puck.png"); _ball->setPosition(Vec2(_screenSize.width * 0.5, _screenSize.height * 0.5 - 2 * _ball->radius())); this->addChild(_ball);
- We will create TTF labels with the
Label
classcreateWithTTF
static method, passing as parameters the initial string value (0
), and the path to the font file. We will then position and rotate the labels:_player1ScoreLabel = Label::createWithTTF("0", "fonts/Arial.ttf", 60); _player1ScoreLabel->setPosition(Vec2(_screenSize.width - 60, _screenSize.height * 0.5 - 80)); _player1ScoreLabel->setRotation(90); this->addChild(_player1ScoreLabel); _player2ScoreLabel = Label::createWithTTF("0", "fonts/Arial.ttf", 60); _player2ScoreLabel->setPosition(Vec2(_screenSize.width - 60, _screenSize.height * 0.5 + 80)); _player2ScoreLabel->setRotation(90); this->addChild(_player2ScoreLabel);
- Then we turn
GameLayer
into a multitouch event listener and tell theDirector
event dispatcher thatGameLayer
wishes to listen to those events. And we finish by scheduling the game's main loop as follows:auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //create main loop this->scheduleUpdate(); return true;
推薦閱讀
- Python編程自學(xué)手冊
- Building Modern Web Applications Using Angular
- 樂高機器人設(shè)計技巧:EV3結(jié)構(gòu)設(shè)計與編程指導(dǎo)
- Learning AndEngine
- Kotlin Standard Library Cookbook
- 琢石成器:Windows環(huán)境下32位匯編語言程序設(shè)計
- 碼上行動:用ChatGPT學(xué)會Python編程
- Learning R for Geospatial Analysis
- Learning Continuous Integration with TeamCity
- Java高并發(fā)核心編程(卷1):NIO、Netty、Redis、ZooKeeper
- LabVIEW虛擬儀器程序設(shè)計從入門到精通(第二版)
- Principles of Strategic Data Science
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Java Web應(yīng)用開發(fā)項目教程
- 硬件產(chǎn)品設(shè)計與開發(fā):從原型到交付