Cocos2dx实现象棋之布局
生活随笔
收集整理的這篇文章主要介紹了
Cocos2dx实现象棋之布局
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
開始界面
#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開始界面實現(xiàn)了滾動布局與點擊選擇執(zhí)黑還是執(zhí)白的功能。
放置棋盤與桌面
//deskCCSprite *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 stoneCCSprite *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);創(chuàng)建棋子類
#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 initif(_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加載棋子類,世界坐標(biāo)與棋盤坐標(biāo)的轉(zhuǎn)換
//addstonefor(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();悔棋功能的實現(xiàn)
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();}結(jié)果展示
總結(jié)
以上是生活随笔為你收集整理的Cocos2dx实现象棋之布局的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Verilog HDL语言设计一个比较电
- 下一篇: ShareSDK集成微信、QQ、微博分享