Chuỗi bài viết hướng dẫn hiện thực lại game ZERO đơn giản. Giúp làm quen cách để hoàn thành 1 game, cũng như cách tổ chức project trong game.
Tiếp tục chuỗi bài viết về hướng dẫn làm game Zero, bài viết này sẽ hướng dẫn cách hiện thực GameScene, cách tính điểm và update điểm số lên màn hình trong lúc chơi game.
Hiện thực
![ss_1](https://resources.stdio.vn/content/article/5ef6226c24fd2869e91f1928/resources/res-1601193055-1601193055090.png)
Tại GameScene.h
Thêm vào 2 biến _score
, và _yourScore
phần private
như sau:
Label* _yourScore; int _score;
Tại GameScene.cpp
Trong hàm init
//score _score = 0; CCString *tempScore = CCString::createWithFormat("%i", _score); _yourScore = Label::createWithBMFont(PATH_FONT_CARBON, "0"); _yourScore->setScale(FONT_SIZE_NORMAL); _yourScore->retain(); _yourScore->setColor(Color3B::ORANGE); _yourScore->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); _yourScore->setPosition(PositionManager::getInstance()->_score); this->addChild(_yourScore, zUI);
Trong hàm inputHandle
Update điểm và set lại string
cho Label
.
void GameScene::inputHandle() { if (_clickedButton == BTN_NONE) return; if ((_resultTrue == true && (_clickedButton == BTN_RIGHT)) || (_resultTrue == false && (_clickedButton == BTN_WRONG))) { _score ++; CCString *tempScore = CCString::createWithFormat("%i", _score); _yourScore->setString(tempScore->getCString()); generateNextChallenge(); } else { Director::getInstance()->replaceScene(GameOverScene::createScene()); } _clickedButton = BTN_NONE; }
Download
Bài chung series
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 1: Tạo Scene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 2: Hiện Thực LoadScene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 3: Thiết Kế Kích Thước Màn Hình, Quản Lý Đối Tượng
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 4: Hiện thực MainMenuScene
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 5: Hiện thực GameScene - Vẽ Suit
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 6: Hiện thực GameScene - Thêm thời gian
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 7: Hiện thực GameScene - Button
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 8: Hiện thực GameScene - Xử lý sự kiện trong game
- Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 9: Hiện thực GameScene - Điểm Số