开始界面
#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(); }