Search…

Hướng Dẫn Viết Game Zero Với Cocos2d-x - Phần 7: Hiện thực GameScene - Button

30/09/20203 min read
Hướng dẫn gắn thêm các button vào game, và hiện thực hàm resetButton cho 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 gắn thêm các button vào game, và hiện thực hàm resetButton cho game.

Hiện thực

Hình ảnh có được sau khi hoàn thành bài viết này:

ss_1

Để có thể hiện thực bên dưới, thêm vài định nghĩa như sau ở file config.cpp

static const char *PATH_SPR_BTN_RIGHT = "spr_btn_right.png";
static const char *PATH_SPR_BTN_RIGHT_PRESS = "spr_btn_right_press.png";
static const char *PATH_SPR_BTN_WRONG = "spr_btn_wrong.png";
static const char *PATH_SPR_BTN_WRONG_PRESS = "spr_btn_wrong_press.png";

enum
{
	BTN_NONE = 0,
	BTN_RIGHT,
	BTN_WRONG
};

Tại GameScene.h

Thêm vào các biến _btnRight_btnWrong_clickedButton, hàm resetButton như sau:

#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__

#include "cocos2d.h"
#include "ui/CocosGUI.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();
	void resetButtons();

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

private:
	Layer           *_layerPresentation;
	ProgressTimer   *_timer;

	int				_nLeftSuits;
	int				_nRightSuits;
	int				_clickedButton;

	Vec2			_centerSuitPostionLeft;
	Vec2			_centerSuitPostionRight;

	float           _currentTime;
	float			_maxTimer;

	cocos2d::ui::Button			*_btnRight;
	cocos2d::ui::Button			*_btnWrong;

	Size visibleSize;
	Vec2 origin;
};

#endif // __GAME_SCENE_H__

Lưu ý: Nhớ là include "ui/CocosGUI.h" vào đầu file.

Tại GameScene.cpp

Trong hàm init

Khởi tạo giá trị cho các biến mới như sau:

_clickedButton = BTN_NONE;
_btnRight = _btnWrong = nullptr;

Gọi hàm resetButton (dòng này phải nằm dưới 2 dòng ở trên):

resetButtons();

Hiện thực hàm resetButton

Hàm này có nhiệm vụ gắn vào 2 button đúng và sai trong game Zero.

void GameScene::resetButtons()
{
	if (_btnRight != nullptr)
	{
		this->removeChild(_btnRight, true);
	}

	if (_btnWrong != nullptr)
	{
		this->removeChild(_btnWrong, true);
	}

	_btnRight = ui::Button::create(PATH_SPR_BTN_RIGHT, PATH_SPR_BTN_RIGHT_PRESS, PATH_SPR_BTN_RIGHT, ui::TextureResType::PLIST);
	_btnRight->setPosition(PositionManager::getInstance()->_right_btn);
	_btnRight->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type){
		switch (type)
		{
		case ui::Widget::TouchEventType::BEGAN:
			break;
		case ui::Widget::TouchEventType::ENDED:
			_clickedButton = BTN_RIGHT;
			break;
		}
	});

	this->addChild(_btnRight, zUI);

	_btnWrong = ui::Button::create(PATH_SPR_BTN_WRONG, PATH_SPR_BTN_WRONG_PRESS, PATH_SPR_BTN_WRONG, ui::TextureResType::PLIST);
	_btnWrong->setPosition(PositionManager::getInstance()->_wrong_btn);
	_btnWrong->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type){
		switch (type)
		{
		case ui::Widget::TouchEventType::BEGAN:
			break;
		case ui::Widget::TouchEventType::ENDED:
			_clickedButton = BTN_WRONG;
			break;
		}
	});

	this->addChild(_btnWrong, zUI);
}

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