擺棋看起來可能會有點(diǎn)煩蒿秦,但是棋子亂擺的話后面會很難管理,所以還是要沉下心把棋子按一定規(guī)則擺好蛋济。
為了擺棋更方便棍鳖,我們需要再給Stone類添加多兩個棋子類型成員變量
- TYPE _type
- Texture2D *texture
在上一篇我們已經(jīng)能夠在棋盤對應(yīng)的位置添加棋子,那么這一篇我們則需要在對應(yīng)的位置添加對應(yīng)的棋子瘫俊。
在這里我們繼續(xù)在initStone寫擺棋的規(guī)則。
initStone函數(shù):
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->_type = proper[id].type;
this->_col = proper[id].col;
this->_row = proper[id].row;
}
else if (id > 8 && id < 16)
{
this->_id = id;
this->_type = proper[id - 9].type;
this->_col = 8 - proper[id - 9].col;
this->_row = proper[id - 9].row;
}
else if (id >= 16 && id <= 24)
{
this->_id = id;
this->_type = proper[id - 16].type;
this->_col = 8 - proper[id - 16].col;
this->_row = 9 - proper[id - 16].row;
}
else if (id > 24)
{
this->_id = id;
this->_type = proper[id - 25].type;
this->_col = proper[id - 25].col;
this->_row = 9 - proper[id - 25].row;
}
//按棋子類型選擇棋子紋理
switch (this->_type)
{
case CHE:
if (_red)
this->setStoneTexture("Stone/rche.png");
else
this->setStoneTexture("Stone/bche.png");
break;
case MA:
if (_red)
this->setStoneTexture("Stone/rma.png");
else
this->setStoneTexture("Stone/bma.png");
break;
case XIANG:
if (_red)
this->setStoneTexture("Stone/rxiang.png");
else
this->setStoneTexture("Stone/bxiang.png");
break;
case SHI:
if (_red)
this->setStoneTexture("Stone/rshi.png");
else
this->setStoneTexture("Stone/bshi.png");
break;
case PAO:
if (_red)
this->setStoneTexture("Stone/rpao.png");
else
this->setStoneTexture("Stone/bpao.png");
break;
case BING:
if (_red)
this->setStoneTexture("Stone/rbing.png");
else
this->setStoneTexture("Stone/bzu.png");
break;
case JIANG:
if (_red)
this->setStoneTexture("Stone/rshuai.png");
else
this->setStoneTexture("Stone/bjiang.png");
break;
default:
break;
}
}
而設(shè)置紋理函數(shù)setStoneTexture是直接從原來init函數(shù)中抽離處理悴灵,以文件路徑字符串作為傳參扛芽,封裝成函數(shù)。
setStoneTexture函數(shù):
void Stone::setStoneTexture(const char* filename)
{
texture = Director::getInstance()->getTextureCache()->addImage(filename);
this->setTexture(texture);
this->setTextureRect(Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
}
至此积瞒,Stone類算是寫好了川尖,我們只需要在LayerGameMain類的addStones通過id創(chuàng)建32個棋子并掛在渲染樹上即可。
LayerGameMain的addStones函數(shù):
void LayerGameMain::addStones()
{
int i = 0;
Stone *stone;
for (i = 0; i < 32; i++)
{
stone = Stone::create(i);
this->addChild(stone);
}
}
最后貼出Stone類的代碼:
Stone.h
#ifndef __STONE_H__
#define __STONE_H__
#include "cocos2d.h"
USING_NS_CC;
/*
布局棋子時因為棋盤左側(cè)及下側(cè)都空出一段茫孔,
所以在布局時叮喳,每個棋子都需要加上左側(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;//棋子的列號
int _row;//棋子的行號
bool _red;//標(biāo)記當(dāng)前棋子顏色
Texture2D *texture;
//枚舉出所有棋子類型
enum TYPE{
CHE,MA,XIANG,SHI,JIANG,PAO,BING
};
TYPE _type;
static Stone* create(int id);
virtual bool init(int id);
void initStone(int id);//該函數(shù)用于初始化棋子成員變量
void setStoneTexture(const char *filename);
Point getPositionFromPlate();//該函數(shù)用于獲取棋子相對于棋盤的位置
};
#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ū)別不大
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->_type = proper[id].type;
this->_col = proper[id].col;
this->_row = proper[id].row;
}
else if (id > 8 && id < 16)
{
this->_id = id;
this->_type = proper[id - 9].type;
this->_col = 8 - proper[id - 9].col;
this->_row = proper[id - 9].row;
}
else if (id >= 16 && id <= 24)
{
this->_id = id;
this->_type = proper[id - 16].type;
this->_col = 8 - proper[id - 16].col;
this->_row = 9 - proper[id - 16].row;
}
else if (id > 24)
{
this->_id = id;
this->_type = proper[id - 25].type;
this->_col = proper[id - 25].col;
this->_row = 9 - proper[id - 25].row;
}
//按棋子類型選擇棋子紋理
switch (this->_type)
{
case CHE:
if (_red)
this->setStoneTexture("Stone/rche.png");
else
this->setStoneTexture("Stone/bche.png");
break;
case MA:
if (_red)
this->setStoneTexture("Stone/rma.png");
else
this->setStoneTexture("Stone/bma.png");
break;
case XIANG:
if (_red)
this->setStoneTexture("Stone/rxiang.png");
else
this->setStoneTexture("Stone/bxiang.png");
break;
case SHI:
if (_red)
this->setStoneTexture("Stone/rshi.png");
else
this->setStoneTexture("Stone/bshi.png");
break;
case PAO:
if (_red)
this->setStoneTexture("Stone/rpao.png");
else
this->setStoneTexture("Stone/bpao.png");
break;
case BING:
if (_red)
this->setStoneTexture("Stone/rbing.png");
else
this->setStoneTexture("Stone/bzu.png");
break;
case JIANG:
if (_red)
this->setStoneTexture("Stone/rshuai.png");
else
this->setStoneTexture("Stone/bjiang.png");
break;
default:
break;
}
}
Point Stone::getPositionFromPlate()
{
Point ret = Point(Stone::_offx + this->_col*_d, Stone::_offy + this->_row*_d);
return ret;
}
void Stone::setStoneTexture(const char* filename)
{
texture = Director::getInstance()->getTextureCache()->addImage(filename);
this->setTexture(texture);
this->setTextureRect(Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
}
運(yùn)行效果: