Cocos2dx實現象棋之佈局

江軍祥發表於2016-02-26

開始介面

#ifndef SCENESTART_H
#define SCENESTART_H
#include "cocos2d.h"
#include "SceneGame.h"
USING_NS_CC;

class SceneStart : public CCLayer
{
public:
    //SceneStart();

   static CCScene * scene();
   CREATE_FUNC(SceneStart)
   bool init();

    bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    void update(float delta);

    CCSprite *_red;
    CCSprite *_black;
    bool _selected;

};

#endif // SCENESTART_H

開始介面實現了滾動佈局與點選選擇執黑還是執白的功能。
開始介面

放置棋盤與桌面

//desk
    CCSprite *desk=CCSprite::create("floor.jpg");
    addChild(desk);
    desk->setPosition(ccp(winSize.width/2,winSize.height/2));
    desk->setScaleX(winSize.width/desk->getContentSize().width);
    desk->setScaleY(winSize.height/desk->getContentSize().height);

    //add plate and  stone

    CCSprite *plate=CCSprite::create("background.png");
    plate->setAnchorPoint(ccp(0,0));
    plate->setPosition(_plateoffset);
    plate->setScale((winSize.height-_plateoffset.y*2)/plate->getContentSize().height);
    addChild(plate);

建立棋子類

#ifndef STONE_H
#define STONE_H
#include "cocos2d.h"
USING_NS_CC;

class Stone : public CCSprite
{
public:
    Stone();
    enum TYPE {JIANG,SHI,XIANG,CHE,MA,PAO,BING};
    static Stone *create(int id,bool red)
    {
        Stone *s=new Stone();
        s->init(id,red);
        s->autorelease();
        return s;
    }
   void reset(bool red);

   bool init(int id,bool red)
   {
       _id=id;
       _red=_id<16;
       //once init
       if(_id < 16)
         _type = _initPos[_id]._type;
         else
        _type = _initPos[_id-16]._type;
       const char* stonePic[14] = {
                   "rshuai.png",
                   "rshi.png",
                   "rxiang.png",
                   "rche.png",
                   "rma.png",
                   "rpao.png",
                   "rbing.png",

                   "bjiang.png",
                   "bshi.png",
                   "bxiang.png",
                   "bche.png",
                   "bma.png",
                   "bpao.png",
                   "bzu.png"
               };
       int idx=(_red?0:1)*7+_type;
       CCSprite::initWithFile(stonePic[idx]);
       setScale(.8f);
       reset(red);
       return true;
   }

    CC_SYNTHESIZE(TYPE,_type,Type)
    CC_SYNTHESIZE(int,_x,X)
    CC_SYNTHESIZE(int,_y,Y)
    CC_SYNTHESIZE(int,_id,Id)
    CC_SYNTHESIZE(bool,_dead,Dead)
    CC_SYNTHESIZE(bool,_red,Red)

    static struct InitPos
    {
        int _x;
        int _y;
        Stone::TYPE _type;
    }_initPos[16];

};

#endif // STONE_H

載入棋子類,世界座標與棋盤座標的轉換

  //addstone
    for(int i=0;i<32;i++)
    {
        _s[i]=Stone::create(i,red);
        addChild(_s[i]);
        //_s[i]->setPosition(ccp(_s[i]->getX()*_d,_s[i]->getY()*_d)+_stoneoffset);
        //setRealPos(_s[i]);
        _s[i]->setPosition(ccp(CCRANDOM_0_1()*winSize.width,CCRANDOM_0_1()*winSize.height));

        CCMoveTo* move=CCMoveTo::create(1,this->getStonePos(_s[i]->getX(), _s[i]->getY()));
        _s[i]->runAction(move);
    }

載入悔棋按鈕

 CCMenu *menu=CCMenu::create();
    CCMenuItemImage *item=CCMenuItemImage::create("regret.jpg","regret.jpg",this,menu_selector(SceneGame::back));
    menu->addChild(item);
    addChild(menu);
    menu->setPositionX(menu->getPositionX()+200);
    _steps=CCArray::create();
    _steps->retain();

悔棋功能的實現

void SceneGame::back(CCObject *)
{
    //CCLog("HELLO WORLD\n");
    if(_steps->count()==0)
        return;
    Step *step=(Step*)_steps->lastObject();
    _s[step->_moveid]->setX(step->_xFrom);
    _s[step->_moveid]->setY(step->_yFrom);
    _s[step->_moveid]->setPosition(getStonePos(step->_xFrom,step->_yFrom));
    if(step->_killid!=-1)
    {
        _s[step->_killid]->setVisible(true);
        _s[step->_killid]->setDead(false);
    }

    _redTrun=!_redTrun;
    _steps->removeLastObject();

}

結果展示

結果

相關文章