天天看點

Cocos3.3橫版遊戲-Part1

在這一部分,我們簡單說明一下檔案的結構.在建立一個檔案的基礎上,加入自制的瓦片地圖.

檔案結構

Cocos3.3橫版遊戲-Part1
Cocos3.3橫版遊戲-Part1

說明:

合理的檔案結構有利于後續開發了解,之前犯了錯,到最後結構混亂修改不便

  一.建立項目

在檔案夾下Shift+右鍵,選擇:在此處打開指令行視窗,建立一個項目

二.擷取資源.

我重新找了主角資源和地圖資源,放在附件連結中.(之後可能會在此更新制作資源方法)

首先是地圖,通過Tiled重新做了一個地圖

Cocos3.3橫版遊戲-Part1

地圖有3層:

第一層是Wall,主角和敵人不能跑到牆上去,設定為7層

第二層是floor,主角主要在這裡活動,設定為3層

第三層是BlackGround,拿來當填充背景的

每隔瓦片設定都是32X32大小的,地圖總大小為80X10

第一步

修改APPDelegate.cpp,符合我們的螢幕大小

<span style="font-family:SimSun;">    auto director = Director::getInstance();
    auto eglView =director->getOpenGLView();
	  if(!eglView) {
        eglView = GLViewImpl::create("My Game");
		eglView->setFrameSize(480,320);
		//WIN32下窗體大小,寬高
        director->setOpenGLView(eglView);
    }
    //director->setOpenGLView(eglView);
	eglView->setDesignResolutionSize(480,320, ResolutionPolicy::SHOW_ALL);</span>
           

第二步

GameScene中添加我們需要的層

<span style="font-family:SimSun;">	auto scene = Scene::create();
	auto gamelayer=GameLayer::create();
	scene->addChild(gamelayer, 0);
	auto operateLayer = OperateLayer::create();
	scene->addChild(operateLayer, 1);
	auto statelayer =StateLayer::create();
	scene->addChild(statelayer);</span>
           

第三步

設定我們的層

GameLayer,遊戲在這裡處理

<span style="font-family:SimSun;">bool GameLayer::init()
{
	bool ret = false;
	do {
		CC_BREAK_IF( !Layer::init());
		_visiblesize = Director::getInstance()->getVisibleSize();
		_origin = Director::getInstance()->getVisibleOrigin();
		this->_viswidth = _visiblesize.width;
		this->_visheight = _visiblesize.height;
		auto map = MapLayer::create();
		this->addChild(map,-100);
		ret = true;
	} while(0);
	return ret;
}</span>
           

OperateLayer,我們的搖杆和按鍵等放在這裡

<span style="font-family:SimSun;">bool OperateLayer::init()
{
	bool ret = false;
	do {
		CC_BREAK_IF( !Layer::init() );
		m_pjoystick = Joystick::create();
		m_pjoystick->setJoystick(Vec2(50,50));
		this->addChild(m_pjoystick);
		ret = true;
	} while(0);
	return ret;
}</span>
           

StateLayer,主角的狀态放在這裡,我們先空着這一層

<span style="font-family:SimSun;">bool StateLayer::init()
{
	bool ret = false;
	do {
		CC_BREAK_IF( !Layer::init() );
		ret = true;
	} while(0);
	return ret;
}</span>
           

其中搖杆的寫法,在這裡我用向量描述,如果你願意的話可以用其他的比如笛卡爾坐标什麼的,還有這個寫法可以分段速度,讓角色實作走動和跑動,貼得核心代碼:

<span style="font-family:SimSun;">void Joystick::updateJoystick(Touch* touch)
{
	Vec2 hit = touch->getLocation();
	float distance = start.getDistance(hit);
	Vec2 direction = (hit - start).getNormalized();
	if(distance < m_pJoystickr/2)
	{
		m_pJoystick->setPosition(start + (direction * distance));
	}else if(distance >m_pJoystickr) {
		m_pJoystick->setPosition(start + (direction * m_pJoystickr));
	}else {
		m_pJoystick->setPosition(start + (direction * m_pJoystickr/2));
	}
}</span>
           

至于地圖的載入方法:

<span style="font-family:SimSun;">bool MapLayer::init()
{
	bool ret = false;
	do {
		CC_BREAK_IF( !Layer::init() );
		this->initMapWithFile("mymap.tmx");
	ret = true;
	} while(0);

	return ret;
}
void MapLayer::initMapWithFile(const char * path)
{
	TMXTiledMap *TileMap = TMXTiledMap::create(path);
	TileMap->setPosition(Vec2(0,0));
	this->addChild(TileMap); 
	global->tileMap = TileMap;
}</span>
           

至此為止,運作之後應該可以看到一個可以拖動的搖杆+地圖了

Cocos3.3橫版遊戲-Part1

繼續閱讀