Cocos2dx实现象棋之布局

前端之家收集整理的这篇文章主要介绍了Cocos2dx实现象棋之布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

开始界面

#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();

}

结果展示

猜你在找的Cocos2d-x相关文章