Search…

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

30/09/20203 min read
Hướng dẫn add thêm thời gian vào game để đếm ngược mỗi lần chơi của game trong Cocos2d-x

Hướng dẫn hiện thực lại game ZERO 1 cách đơ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.

Hướng dẫn add thêm thời gian vào game để đếm ngược mỗi lần chơi của game.

Hiện thực

Trước khi hiện thực GameScene vào file config.h định nghĩa thêm để được như sau:

#ifndef __CONFIG_H__
#define __CONFIG_H__

#include "cocos2d.h"
USING_NS_CC;

#define TRANSITION_TIME 1.0f

static const float      SCREEN_DESIGN_WIDTH     = 1080.0f;
static const float      SCREEN_DESIGN_HEIGHT    = 1920.0f;

static const char *PATH_SPR_BOARD = "spr_board.png";
static const char *PATH_SPR_CLOCK = "spr_clock.png";

static const char *PATH_CONF_POS = "conf_objects_pos.plist";

static const char *PATH_SPR_BTN_PLAY = "spr_btn_play.png";
static const char *PATH_SPR_BTN_PLAY_PRESS = "spr_btn_play_press.png";
static const char *PATH_SPR_TITLE_ZERO = "spr_title_zero.png";

static const int        TIMER_OPACITY = 255;

static const float      FP_EPSILON = 0.000001f;
static const float      MIN_SUITS_PER_SIDE = 1;
static const float      MAX_SUITS_PER_SIDE = 4;

static const float SUIT_DISTANCE = 80.0f;

static const char       *PATH_SPR_SUITS[] =
{
	"spr_suit_spade.png",
	"spr_suit_heart.png",
	"spr_suit_diamond.png",
	"spr_suit_club.png"
};

enum
{
	zBACKGROUND = 0,
	zGAME_BOARD,
	zGAME_PLAY,
	zUI,
};

enum
{
	SUIT_SPADE = 0,
	SUIT_HEART,
	SUIT_DIAMOND,
	SUIT_CLUB
};

#endif // __CONFIG_H__

Tại GameScene.h

Tại đây thêm vào các thuộc tính _timer, _maxTimer, _currentTime. Cũng như hàm update của scene:

#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;

class GameScene : public cocos2d::Layer
{
public:
	// there's no 'id' in cpp, so we recommend returning the class instance pointer
	static cocos2d::Scene* createScene();

	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();

	void update(float);
	void generateNextChallenge();

	// implement the "static create()" method manually
	CREATE_FUNC(GameScene);

private:
	Layer           *_layerPresentation;
	ProgressTimer   *_timer;

	int				_nLeftSuits;
	int				_nRightSuits;

	Vec2			_centerSuitPostionLeft;
	Vec2			_centerSuitPostionRight;

	float           _currentTime;
	float			_maxTimer;

	Size visibleSize;
	Vec2 origin;
};

#endif // __GAME_SCENE_H__

Tại GameScene.cpp

Trong hàm init

Thêm timer

//add timer
_timer = ProgressTimer::create(Sprite::createWithSpriteFrameName(PATH_SPR_CLOCK));
_timer->setReverseDirection(true);
_timer->setPosition(PositionManager::getInstance()->_timer);
this->addChild(_timer, zGAME_PLAY);

Gọi hàm update cho mỗi frame

// add this scene to scheduler to update clock
this->scheduleUpdate();

Trong hàm generateNextChallenge

Khởi tạo _currentTime, _maxTimer, setColor cho _timer để được như sau:

void GameScene::generateNextChallenge()
{
	_layerPresentation->removeAllChildrenWithCleanup(true);
	_currentTime = _maxTimer = 1.5f;
	_timer->setColor(Color3B::WHITE);

	// randomize number of every suit
    _nLeftSuits = MIN_SUITS_PER_SIDE + CCRANDOM_0_1()*(MAX_SUITS_PER_SIDE - MIN_SUITS_PER_SIDE + 1 - FP_EPSILON);
    _nRightSuits = MIN_SUITS_PER_SIDE + CCRANDOM_0_1()*(MAX_SUITS_PER_SIDE - MIN_SUITS_PER_SIDE + 1 - FP_EPSILON);

	drawSuitsTable(_nLeftSuits, _centerSuitPostionLeft, origin.x, origin.y, _layerPresentation);
	drawSuitsTable(_nRightSuits, _centerSuitPostionRight, origin.x, origin.y, _layerPresentation);
}

Hiện thực hàm update

void GameScene::update(float dt)
{
	_currentTime -= dt;

	if (_currentTime < 0)
	{
		_currentTime = 0.0f;
		// chuyển scene
	}

	float percentage = _currentTime*100.0f / _maxTimer;
	_timer->setPercentage(percentage);
	_timer->setOpacity(TIMER_OPACITY);
	if (percentage < 50.0f)
	{
		_timer->setColor(Color3B(255, 255 * percentage / 50, 255 * percentage / 50));
	}
}

Download

SOURCE_CODE

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