Search…

Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 9: Hiện thực GameScene - Điểm Số

30/09/20202 min read
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.

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

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

SOURCE_CODE

RESOURCE

Bài chung series

IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024