在這里我們需要給Stone類(lèi)添加多幾個(gè)成員變量以方便后面解決棋子擺放問(wèn)題拣技。
- bool _red 顏色標(biāo)記瞧甩,判斷當(dāng)前棋子是否為紅棋
- int _row 記錄棋子行號(hào)熄捍,行號(hào)不大于9
- int _col 記錄棋子列號(hào)儿普,列號(hào)不大于8
- int _id 記錄棋子的id祖娘,id號(hào)不大于32
因?yàn)閯?chuàng)建棋子時(shí)需要傳入棋子的id,所以我們對(duì)Stone類(lèi)的create函數(shù)及init函數(shù)稍做修改妹窖。(create函數(shù)原來(lái)由cocos的工廠(chǎng)方法CREATE_FUNC幫我們創(chuàng)建纬朝,我們重寫(xiě)時(shí)可以參考一下CREATE_FUNC的寫(xiě)法)
bool Stone::init(int id)
{
if (!Sprite::init())
{
return false;
}
Sprite *stone = Sprite::create("Stone/rche.png");
stone->setPosition(getPositionFromPlate());
this->addChild(stone);
return true;
}
Stone* Stone::create(int id)
{
Stone *ret = new Stone();
if (!ret && ret->init(id))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
return ret;
}
擺棋分析:
擺棋前我們要引入一個(gè)對(duì)稱(chēng)的概念,我們可以把棋盤(pán)平分為四塊矩形骄呼,而這四塊矩形上只要擺好第一塊矩形上的棋子共苛,那么另外三塊即可通過(guò)對(duì)稱(chēng)或旋轉(zhuǎn)和控制棋子顏色獲得,在這里我們需要用枚舉的方法列出棋子的類(lèi)型蜓萄,然后通過(guò)棋子的類(lèi)型記錄棋子在第一塊矩形相應(yīng)的行號(hào)及列號(hào)隅茎,接著我們就可以通過(guò)旋轉(zhuǎn)和棋子的顏色標(biāo)記來(lái)設(shè)置剩下的棋子的位置及紋理。
我們先確定第一塊矩形的棋子的行列嫉沽,以方便我們后面的對(duì)稱(chēng)和旋轉(zhuǎn)擺棋辟犀。如圖。
然后在Stone.h中枚舉出各類(lèi)棋子
enum TYPE{
CHE,MA,XIANG,SHI,JIANG,PAO,BING
};
接著通過(guò)一個(gè)結(jié)構(gòu)體記錄第一塊矩形的行號(hào)和列號(hào)(該結(jié)構(gòu)體放在了初始化棋子信息的函數(shù)里绸硕,后面會(huì)提到)
struct
{
TYPE type;
int row;
int col;
} proper[9] = {
{CHE, 0, 0},
{MA, 0, 1},
{XIANG, 0, 2},
{SHI, 0, 3},
{BING, 3, 2},
{BING, 3, 0},
{PAO, 2, 1},
{JIANG, 0, 4},
{BING, 3, 4}
};
有了前面的邏輯后堂竟,我們可以開(kāi)始寫(xiě)一個(gè)initStone來(lái)初始化棋子信息
void Stone::initStone(int id)
{
struct
{
TYPE type;
int col;
int row;
}proper[9] = {
{CHE,0,0},
{MA,0,1},
{XIANG,0,2},
{SHI,0,3},
{BING,3,2},
{BING,3,0},
{PAO,2,1},
{JIANG,0,4},
{BING,3,4}
};
_red = id < 16;
if (id < 8)
{
this->_id = id;
this->_col = proper[id].col;
this->_row = proper[id].row;
}
}
因?yàn)樘砑恿诵辛谐蓡T變量,相應(yīng)地改一下原來(lái)的getPositionFromPlate函數(shù)
Point Stone::getPositionFromPlate()
{
Point ret = Point(Stone::_offx + this->_row*_d, Stone::_offy + this->_col*_d);
return ret;
}
因?yàn)楸救吮磉_(dá)能力有限玻佩,為了避免混亂或有人看不懂跃捣,在最后就把Stone類(lèi)修改后的代碼貼出來(lái)。
Stone.h
#ifndef __STONE_H__
#define __STONE_H__
#include "cocos2d.h"
USING_NS_CC;
/*
布局棋子時(shí)因?yàn)槠灞P(pán)左側(cè)及下側(cè)都空出一段夺蛇,
所以在布局時(shí),每個(gè)棋子都需要加上左側(cè)空白段_offx及下側(cè)空白段_offy酣胀。
*/
class Stone : public Sprite
{
public:
static int _d;//棋子直徑
static int _offx;//棋子左側(cè)空白段
static int _offy;//棋子下側(cè)空白段
int _id;//棋子id
int _col;//棋子的列號(hào)
int _row;//棋子的行號(hào)
bool _red;//標(biāo)記當(dāng)前棋子顏色
//枚舉出所有棋子類(lèi)型
enum TYPE{
CHE,MA,XIANG,SHI,JIANG,PAO,BING
};
static Stone* create(int id);
virtual bool init(int id);
void initStone(int id);//該函數(shù)用于初始化棋子成員變量
Point getPositionFromPlate();//該函數(shù)用于獲取棋子相對(duì)于棋盤(pán)的位置
};
#endif
Stone.cpp
#include "Stone.h"
int Stone::_d = 32;
int Stone::_offx = 32;
int Stone::_offy = 16;
bool Stone::init(int id)
{
if (!Sprite::init())
{
return false;
}
//設(shè)置紋理圖片刁赦,此處與2.x版本有點(diǎn)區(qū)別,但是區(qū)別不大
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage("Stone/rche.png");
this->setTexture(texture);
this->setTextureRect(Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
initStone(id);
setPosition(getPositionFromPlate());
return true;
}
Stone* Stone::create(int id)
{
Stone *ret = new Stone();
if (ret && ret->init(id))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
return ret;
}
void Stone::initStone(int id)
{
struct
{
TYPE type;
int row;
int col;
}proper[9] = {
{CHE,0,0},
{MA,0,1},
{XIANG,0,2},
{SHI,0,3},
{BING,3,2},
{BING,3,0},
{PAO,2,1},
{JIANG,0,4},
{BING,3,4}
};
_red = id < 16;
if (id < 8)
{
this->_id = id;
this->_col = proper[_id].col;
this->_row = proper[_id].row;
}
/*此處添加后續(xù)擺棋代碼*/
}
Point Stone::getPositionFromPlate()
{
Point ret = Point(Stone::_offx + this->_col*_d, Stone::_offy + this->_row*_d);
return ret;
}
此時(shí)可以通過(guò)修改id號(hào)(不大于8)擺放車(chē)的位置
運(yùn)行圖: