标签:style blog http io color ar os for sp
第一版出来了
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 #include <map> 6 #include <vector> 7 8 USING_NS_CC; 9 10 typedef unsigned char BYTE; 11 12 class HelloWorld : public cocos2d::Layer 13 { 14 public: 15 // there‘s no ‘id‘ in cpp, so we recommend returning the class instance pointer 16 static cocos2d::Scene* createScene(); 17 18 // Here‘s a difference. Method ‘init‘ in cocos2d-x returns bool, instead of returning ‘id‘ in cocos2d-iphone 19 virtual bool init(); 20 21 // a selector callback 22 void menuCloseCallback(cocos2d::Ref* pSender); 23 24 // implement the "static create()" method manually 25 CREATE_FUNC(HelloWorld); 26 }; 27 28 class ChessPiece : public cocos2d::Sprite 29 { 30 public: 31 virtual bool init(); 32 33 //设置队伍 1 2 34 void setType(BYTE type); 35 static ChessPiece* pieceWithType(BYTE type); 36 void setChosed(bool chose); 37 bool getChosed(); 38 bool checkIsTouch(Point pos); 39 protected: 40 BYTE mNowType; 41 Node * mChosedShow;//选中标志 42 }; 43 44 #define BOARD_LINE_NUM 4 45 class Chessboard : public cocos2d::Node 46 { 47 public: 48 virtual bool init(); 49 50 CREATE_FUNC(Chessboard); 51 52 //重置棋盘 53 void reSet(); 54 virtual void onEnter(); 55 bool onTouchBegan(Touch* touch, Event* event); 56 void onTouchMoved(Touch* touch, Event* event); 57 void onTouchEnded(Touch* touch, Event* event); 58 59 protected: 60 Point maBoardPoints[BOARD_LINE_NUM][BOARD_LINE_NUM]; 61 BYTE maBoardData[BOARD_LINE_NUM][BOARD_LINE_NUM]; 62 int miTeam1LeftNum; 63 int miTeam2LeftNum; 64 bool mbTeam1Turn;//是否是改队伍1 下棋了 65 bool mbGameOver; 66 Sprite * maStateSp[2]; 67 68 std::map<int,ChessPiece*> mPieces; 69 Node * maCanWalkShow[BOARD_LINE_NUM][BOARD_LINE_NUM]; 70 int miLastChosedIndex; 71 72 Point parseIndexToPos(int index); 73 int parsePosToIndex(Point pos); 74 std::vector<Point> getCanWalkPosWithIndex(int index); 75 76 std::vector<Point> mvCanWalkPos; 77 void showCanWalkPos(bool bShow); 78 void checkDieWithPos(Point pos); 79 int checkDiePos(int x1,int x2,int x3,int x4,int key); 80 void someOneDie(int iX,int iY); 81 void checkGameEndWithNumAndTeam(int iNum,int iTeam); 82 bool checkIsOnTurn(int iTeam); 83 bool checkCanMoveHere(int x1,int y1,int x2,int y2); 84 85 void refreshStateFlag(); 86 void onPressReset(cocos2d::Ref* pSender); 87 }; 88 89 90 91 92 #endif // __HELLOWORLD_SCENE_H__
1 #include "HelloWorldScene.h" 2 3 bool Chessboard::init() 4 { 5 ////////////////////////////// 6 // 1. super init first 7 if ( !Node::init() ) 8 { 9 return false; 10 } 11 12 Size visibleSize = Director::getInstance()->getVisibleSize(); 13 Vec2 origin = Director::getInstance()->getVisibleOrigin(); 14 15 miLastChosedIndex = -1; 16 17 //棋盘底图 18 int i = 0 , j = 0; 19 auto bg = Sprite::create("muwen.png"); 20 if( bg != NULL ) 21 { 22 int iX = 0, iY = 0, iW = 0, iH = 0 ; 23 iW = bg->getContentSize().width; 24 iH = bg->getContentSize().height; 25 iX = ceil(visibleSize.width/iW); 26 iY = ceil(visibleSize.height/iH); 27 28 for ( i = 0; i < iX; i++ ) 29 { 30 for ( j = 0; j < iY; j++ ) 31 { 32 auto temp = Sprite::create("muwen.png"); 33 temp->setAnchorPoint(CCPointZero); 34 temp->setPosition(ccp(i*iW,j*iH)); 35 addChild(temp); 36 } 37 } 38 } 39 40 //当前 41 LabelTTF * pCurrent = LabelTTF::create("now", "fonts/arial.ttf", 20); 42 pCurrent->setPosition( ccp(20,visibleSize.height - 20 ) ); 43 pCurrent->setAnchorPoint( ccp(0,1)); 44 addChild(pCurrent); 45 46 Sprite * pNow1 = Sprite::create("white_point.png"); 47 pNow1->setPosition( ccp(20,visibleSize.height - 50 ) ); 48 pNow1->setAnchorPoint( ccp(0,1)); 49 addChild(pNow1); 50 maStateSp[0] = pNow1; 51 52 pNow1 = Sprite::create("black_point.png"); 53 pNow1->setPosition( ccp(20,visibleSize.height - 50 ) ); 54 pNow1->setAnchorPoint( ccp(0,1)); 55 addChild(pNow1); 56 maStateSp[1] = pNow1; 57 58 //棋牌黑框 3*3 59 auto cell = Sprite::create("point.png"); 60 if( cell != NULL ) 61 { 62 int iLineW = 18; 63 int x = visibleSize.width/2 - 1.5*cell->getContentSize().width; 64 int y = visibleSize.height/2 - 1.5*cell->getContentSize().height; 65 for ( i = 0; i < (BOARD_LINE_NUM - 1) ; i++ ) 66 { 67 for ( j = 0; j < (BOARD_LINE_NUM -1) ; j++ ) 68 { 69 auto temp = Sprite::create("point.png"); 70 temp->setAnchorPoint(CCPointZero); 71 Point pos = ccp(x+1*iLineW+i*(cell->getContentSize().width-iLineW),y+1*iLineW+j*(cell->getContentSize().height-iLineW)); 72 temp->setPosition(pos); 73 addChild(temp); 74 maBoardPoints[i][j] = ccp(pos.x+8,pos.y+8); 75 } 76 } 77 } 78 79 //初始化外围的点的坐标 80 for ( i = 0; i < (BOARD_LINE_NUM -1) ; i++ ) 81 { 82 maBoardPoints[i][(BOARD_LINE_NUM - 1)].x = maBoardPoints[i][2].x; 83 maBoardPoints[i][(BOARD_LINE_NUM - 1)].y = maBoardPoints[i][2].y + (maBoardPoints[i][2].y - maBoardPoints[i][1].y); 84 85 maBoardPoints[(BOARD_LINE_NUM - 1)][i].x = maBoardPoints[2][i].x + (maBoardPoints[2][i].x - maBoardPoints[1][i].x); 86 maBoardPoints[(BOARD_LINE_NUM - 1)][i].y = maBoardPoints[2][i].y ; 87 } 88 89 maBoardPoints[i][(BOARD_LINE_NUM - 1)].x = maBoardPoints[2][2].x + (maBoardPoints[2][2].x - maBoardPoints[1][2].x); 90 maBoardPoints[i][(BOARD_LINE_NUM - 1)].y = maBoardPoints[2][2].y + (maBoardPoints[2][2].y - maBoardPoints[2][1].y); 91 92 93 for ( i = 0; i < BOARD_LINE_NUM; i++ ) 94 { 95 for ( j = 0; j < BOARD_LINE_NUM; j++ ) 96 { 97 { 98 LayerColor * piece = LayerColor::create(Color4B(0, 255, 0, 200), 40, 40); 99 piece->setAnchorPoint(ccp(0.5f,0.5f)); 100 piece->ignoreAnchorPointForPosition(false); 101 piece->setPosition(maBoardPoints[i][j]); 102 addChild(piece); 103 piece->setVisible(false); 104 maCanWalkShow[i][j] = piece; 105 } 106 } 107 } 108 reSet(); 109 110 111 MenuItemFont::setFontSize(20); 112 auto closeItem = MenuItemFont::create("reset",CC_CALLBACK_1(Chessboard::onPressReset, this)); 113 closeItem->setPosition( ccp(20,visibleSize.height - 150 ) ); 114 closeItem->setAnchorPoint( ccp(0,1)); 115 116 //closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 117 // origin.y + closeItem->getContentSize().height/2)); 118 119 // create menu, it‘s an autorelease object 120 auto menu = Menu::create(closeItem, NULL); 121 menu->setPosition(Vec2::ZERO); 122 this->addChild(menu, 1); 123 124 125 return true; 126 } 127 void Chessboard::onPressReset(cocos2d::Ref* pSender) 128 { 129 reSet(); 130 } 131 void Chessboard::onEnter() 132 { 133 Node::onEnter(); 134 135 // Register Touch Event 136 auto listener = EventListenerTouchOneByOne::create(); 137 listener->setSwallowTouches(true); 138 139 listener->onTouchBegan = CC_CALLBACK_2(Chessboard::onTouchBegan, this); 140 listener->onTouchMoved = CC_CALLBACK_2(Chessboard::onTouchMoved, this); 141 listener->onTouchEnded = CC_CALLBACK_2(Chessboard::onTouchEnded, this); 142 143 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); 144 } 145 bool Chessboard::onTouchBegan(Touch* touch, Event* event) 146 { 147 // CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y); 148 int w = maBoardPoints[1][0].x - maBoardPoints[0][0].x; 149 int h = maBoardPoints[0][1].y - maBoardPoints[0][0].y; 150 Point relativePos = ccp( touch->getLocation().x - maBoardPoints[0][0].x,touch->getLocation().y - maBoardPoints[0][0].y ); 151 //CCLOG("relativePos: x=%f y=%f",relativePos.x,relativePos.y); 152 int iX = int ( relativePos.x / w + 0.5 ); 153 int iDisX = (int)relativePos.x%w; 154 155 int iY = int ( relativePos.y / h + 0.5 ); 156 int iDisY = (int)relativePos.y%h; 157 158 bool bChosed = false; 159 int radius = 40; 160 if( iDisX < radius && iDisY < radius || 161 iDisX < radius && iDisY > h - radius || 162 iDisX > w - radius && iDisY > iDisY < radius || 163 iDisX > w - radius && iDisY > h - radius 164 ) 165 { 166 CCLOG("TOUCH: x=%d y=%d",iX,iY); 167 int choseIndex = parsePosToIndex(ccp(iX,iY)); 168 //如果是点中了格子 169 if( 0 == maBoardData[iX][iY] ) 170 {//点中了空白格子 171 Point old = parseIndexToPos(miLastChosedIndex); 172 if( miLastChosedIndex >= 0 && checkCanMoveHere(old.x,old.y,iX,iY) ) 173 {//将选中棋子移过来 174 std::map<int,ChessPiece*>::iterator it = mPieces.find( miLastChosedIndex ); 175 if( it != mPieces.end() ) 176 { 177 //更新数据层 178 int iTeam = maBoardData[(int)old.x][(int)old.y]; 179 maBoardData[(int)old.x][(int)old.y] = 0; 180 maBoardData[iX][iY] = iTeam; 181 miLastChosedIndex = -1; 182 183 //更新ui层 184 185 it->second->runAction(Sequence::createWithTwoActions( 186 MoveTo::create(0.3f,maBoardPoints[iX][iY]), 187 CallFunc::create( CC_CALLBACK_0(Chessboard::checkDieWithPos, this,ccp(iX,iY))) 188 )); 189 ChessPiece* pTemp = it->second; 190 pTemp->setChosed(false); 191 mPieces.erase(it); 192 mPieces[choseIndex] = pTemp; 193 194 mbTeam1Turn = !mbTeam1Turn; 195 refreshStateFlag(); 196 //checkDieWithPos(ccp(iX,iY),iTeam); 197 } 198 } 199 } 200 else 201 {//点中了棋子 202 if( checkIsOnTurn(maBoardData[iX][iY]) ) 203 { 204 if(miLastChosedIndex >= 0) 205 { 206 mPieces[miLastChosedIndex]->setChosed(false); 207 } 208 209 showCanWalkPos(false); 210 mvCanWalkPos = getCanWalkPosWithIndex(parsePosToIndex(ccp(iX,iY))); 211 showCanWalkPos(true); 212 213 std::map<int,ChessPiece*>::iterator it = mPieces.find( choseIndex ); 214 if( it != mPieces.end() ) 215 { 216 it->second->setChosed(true); 217 miLastChosedIndex = choseIndex; 218 bChosed = true; 219 } 220 } 221 } 222 } 223 224 if(false == bChosed) 225 { 226 showCanWalkPos(false); 227 if(miLastChosedIndex >= 0) 228 { 229 mPieces[miLastChosedIndex]->setChosed(false); 230 } 231 232 miLastChosedIndex = -1; 233 } 234 return true; 235 } 236 237 void Chessboard::onTouchMoved(Touch* touch, Event* event) 238 { 239 // CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y); 240 } 241 242 void Chessboard::onTouchEnded(Touch* touch, Event* event) 243 { 244 245 } 246 bool Chessboard::checkCanMoveHere(int x1,int y1,int x2,int y2) 247 { 248 bool bRet = false; 249 int disX = abs(x1-x2); 250 int disY = abs(y1-y2); 251 if( disY <= 1 && disX <= 1 && disX != disY ) 252 { 253 bRet = true; 254 } 255 return bRet; 256 } 257 void Chessboard::checkDieWithPos(Point pos) 258 { 259 //先判断横向 260 int x = (int)pos.x; 261 int y = (int)pos.y; 262 int iDieX = checkDiePos(maBoardData[0][y],maBoardData[1][y],maBoardData[2][y],maBoardData[3][y],maBoardData[x][y]); 263 if( iDieX != -1 ) 264 { 265 someOneDie(iDieX,y); 266 } 267 268 int iDieY = checkDiePos(maBoardData[x][0],maBoardData[x][1],maBoardData[x][2],maBoardData[x][3],maBoardData[x][y]); 269 if( iDieY != -1 ) 270 { 271 someOneDie(x,iDieY); 272 } 273 } 274 void Chessboard::someOneDie(int iX,int iY) 275 { 276 //删除数据层 277 int iTeam = maBoardData[iX%BOARD_LINE_NUM][iY%BOARD_LINE_NUM]; 278 if( 1 == iTeam ) 279 { 280 miTeam1LeftNum--; 281 checkGameEndWithNumAndTeam(miTeam1LeftNum,1); 282 } 283 else if( 2 == iTeam ) 284 { 285 miTeam2LeftNum--; 286 checkGameEndWithNumAndTeam(miTeam2LeftNum,2); 287 } 288 289 maBoardData[iX%BOARD_LINE_NUM][iY%BOARD_LINE_NUM] = 0; 290 291 //删除ui层 292 std::map<int,ChessPiece*>::iterator it = mPieces.find( parsePosToIndex(ccp(iX,iY)) ); 293 if ( it != mPieces.end() ) 294 { 295 it->second->removeFromParent(); 296 mPieces.erase(it); 297 } 298 } 299 void Chessboard::refreshStateFlag() 300 { 301 maStateSp[0]->setVisible(mbTeam1Turn); 302 maStateSp[1]->setVisible(!mbTeam1Turn); 303 } 304 void Chessboard::checkGameEndWithNumAndTeam(int iNum,int iTeam) 305 { 306 if( iNum <= 1 && false == mbGameOver ) 307 { 308 mbGameOver = true; 309 CCLOG("team:%d is die ,game ove!!",iTeam); 310 //String sShow; 311 //sShow.initWithFormat( 312 std::string sShow;; 313 if( 1 == iTeam ) 314 { 315 sShow.assign("游戏结束!黑方获胜^^!"); 316 } 317 else 318 { 319 sShow.assign("游戏结束!白方获胜^^!"); 320 } 321 cocos2d::MessageBox( sShow.c_str(),"系统" ); 322 } 323 } 324 /* 325 四个位置的 team id,key为 活动的棋子的team id( 1:队伍1 2:队伍2 0:空白) 326 检查当前是否有棋子被吃了 如果返回-1表明木有伤亡,其他则表示 位置第几的棋子被吃了。。 327 2110 328 0211 329 1120 330 0112 331 */ 332 int Chessboard::checkDiePos(int x1,int x2,int x3,int x4,int key) 333 { 334 int iRet = -1; 335 if( x1 != key && x1 != 0 && x2 == key && x3 == key && 0 == x4 ) 336 { 337 iRet = 0; 338 } 339 else if( 0 == x1 && x2 != key && x2 != 0 && x3 == key && x4 == key ) 340 { 341 iRet = 1; 342 } 343 else if( x1 == key && x2 == key && x3 != key && x3 != 0 && 0 == x4 ) 344 { 345 iRet = 2; 346 } 347 else if( 0 == x1 && x2 == key && x3 == key && x4 != key && x4 != 0 ) 348 { 349 iRet = 3; 350 } 351 return iRet; 352 } 353 bool Chessboard::checkIsOnTurn(int iTeam) 354 { 355 bool bRet = false; 356 if(mbTeam1Turn) 357 { 358 if( 1 == iTeam ) 359 { 360 bRet = true; 361 } 362 } 363 else 364 { 365 if( 2 == iTeam ) 366 { 367 bRet = true; 368 } 369 } 370 return bRet; 371 } 372 void Chessboard::reSet() 373 { 374 //清除所有棋子 375 std::map<int,ChessPiece*>::iterator it; 376 for ( it = mPieces.begin() ; it != mPieces.end() ; ++ it ) 377 { 378 it->second->removeFromParent(); 379 } 380 mPieces.clear(); 381 382 int i = 0 , j = 0; 383 memset(maBoardData,0,sizeof(maBoardData)); 384 //初始化1队 385 for ( i = 0; i < 4; i++ ) 386 { 387 maBoardData[i][3] = 1; 388 } 389 maBoardData[0][2] = 1; 390 maBoardData[3][2] = 1; 391 392 //初始化2队 393 for ( i = 0; i < 4; i++ ) 394 { 395 maBoardData[i][0] = 2; 396 } 397 maBoardData[0][1] = 2; 398 maBoardData[3][1] = 2; 399 400 miTeam1LeftNum = 6; 401 miTeam2LeftNum = 6; 402 403 mbGameOver = false; 404 405 mbTeam1Turn = true; 406 407 refreshStateFlag(); 408 //棋子 409 for ( i = 0; i < BOARD_LINE_NUM; i++ ) 410 { 411 for ( j = 0; j < BOARD_LINE_NUM; j++ ) 412 { 413 if( 1 == maBoardData[i][j] ) 414 { 415 auto piece = ChessPiece::pieceWithType(1); 416 piece->setPosition(maBoardPoints[i][j]); 417 addChild(piece); 418 mPieces[(BOARD_LINE_NUM*i+j)] = piece; 419 } 420 else if( 2 == maBoardData[i][j] ) 421 { 422 auto piece = ChessPiece::pieceWithType(2); 423 piece->setPosition(maBoardPoints[i][j]); 424 addChild(piece); 425 mPieces[(BOARD_LINE_NUM*i+j)] = piece; 426 } 427 } 428 } 429 } 430 Point Chessboard::parseIndexToPos(int index) 431 { 432 return ccp(index/BOARD_LINE_NUM,index%BOARD_LINE_NUM); 433 } 434 int Chessboard::parsePosToIndex(Point pos) 435 { 436 return pos.x*BOARD_LINE_NUM + pos.y; 437 } 438 std::vector<Point> Chessboard::getCanWalkPosWithIndex(int index) 439 { 440 std::vector<Point> vRet; 441 Point pos = parseIndexToPos(index); 442 //up 443 if( pos.y + 1 < BOARD_LINE_NUM ) 444 {//再检查是否已经有其他棋子了 445 if( 0 == maBoardData[(int)pos.x][(int)pos.y+1] ) 446 { 447 vRet.push_back(ccp(pos.x,pos.y+1)); 448 } 449 } 450 451 //down 452 if( pos.y - 1 >= 0 ) 453 {//再检查是否已经有其他棋子了 454 if( 0 == maBoardData[(int)pos.x][(int)pos.y-1] ) 455 { 456 vRet.push_back(ccp(pos.x,pos.y-1)); 457 } 458 } 459 460 //left 461 if( pos.x - 1 >= 0 ) 462 {//再检查是否已经有其他棋子了 463 if( 0 == maBoardData[(int)pos.x - 1][(int)pos.y] ) 464 { 465 vRet.push_back(ccp(pos.x-1,pos.y)); 466 } 467 } 468 469 //right 470 if( pos.x + 1 < BOARD_LINE_NUM ) 471 {//再检查是否已经有其他棋子了 472 if( 0 == maBoardData[(int)pos.x + 1][(int)pos.y] ) 473 { 474 vRet.push_back(ccp(pos.x+1,pos.y)); 475 } 476 } 477 478 return vRet; 479 } 480 void Chessboard::showCanWalkPos(bool bShow) 481 { 482 for (int i = 0; i < mvCanWalkPos.size(); i++) 483 { 484 Point pos = mvCanWalkPos.at(i); 485 maCanWalkShow[(int)pos.x%BOARD_LINE_NUM][(int)pos.y%BOARD_LINE_NUM]->setVisible(bShow); 486 } 487 } 488 ChessPiece* ChessPiece::pieceWithType(BYTE type) 489 { 490 ChessPiece* pPiece = new ChessPiece(); 491 pPiece->init(); 492 pPiece->setType(type); 493 pPiece->autorelease(); 494 return pPiece; 495 } 496 bool ChessPiece::init() 497 { 498 ////////////////////////////// 499 // 1. super init first 500 if ( !Sprite::init() ) 501 { 502 return false; 503 } 504 505 initWithTexture(Sprite::create("black_point.png")->getTexture()); 506 507 mChosedShow = Node::create(); 508 mChosedShow->setAnchorPoint(ccp(0.5f,0.5f)); 509 mChosedShow->setPosition(Vec2(getContentSize().width/2.0f,getContentSize().height/2.0f)); 510 addChild(mChosedShow); 511 int radio = 20; 512 int iTipL = 15; 513 514 Sprite * l = Sprite::create("green.png"); 515 l->setAnchorPoint(Vec2(1.0f, 0.5f)); 516 l->setPosition(Vec2(-radio,0)); 517 mChosedShow->addChild(l); 518 519 Sprite * r = Sprite::create("green.png"); 520 r->setAnchorPoint(Vec2(0.0f, 0.5f)); 521 r->setPosition(Vec2(radio,0)); 522 mChosedShow->addChild(r); 523 524 Sprite * u = Sprite::create("green2.png"); 525 u->setAnchorPoint(Vec2(0.5f, 0.0f)); 526 u->setPosition(Vec2(0,radio)); 527 mChosedShow->addChild(u); 528 529 Sprite * d = Sprite::create("green2.png"); 530 d->setAnchorPoint(Vec2(0.5f, 1.0f)); 531 d->setPosition(Vec2(0,-radio)); 532 mChosedShow->addChild(d); 533 534 mChosedShow->setVisible(false); 535 536 mNowType = 0; 537 538 setType(1); 539 540 return true; 541 } 542 //设置队伍 1 2 543 void ChessPiece::setType(BYTE type) 544 { 545 if( type != mNowType ) 546 { 547 if( 1 == type ) 548 { 549 //initWithFile("white_point.png"); 550 setTexture( Sprite::create("white_point.png")->getTexture()); 551 } 552 else 553 { 554 //initWithFile("black_point.png"); 555 setTexture( Sprite::create("black_point.png")->getTexture()); 556 } 557 mNowType = type; 558 } 559 } 560 561 //设置选中棋子 562 void ChessPiece::setChosed(bool chose) 563 { 564 //目前就在棋子的周围显示绿色作为选中标志 565 mChosedShow->setVisible(chose); 566 } 567 bool ChessPiece::getChosed() 568 { 569 return mChosedShow->isVisible(); 570 } 571 bool ChessPiece::checkIsTouch(Point pos) 572 { 573 bool bRet = false; 574 int iH = 25; 575 Point now = this->getPosition(); 576 if( (now.x-iH) <= pos.x && 577 pos.x <= (now.x+iH) && 578 (now.y-iH) <= pos.y && 579 pos.y <= (now.y+iH)) 580 { 581 bRet = true; 582 } 583 return bRet; 584 } 585 Scene* HelloWorld::createScene() 586 { 587 // ‘scene‘ is an autorelease object 588 auto scene = Scene::create(); 589 590 // ‘layer‘ is an autorelease object 591 auto layer = HelloWorld::create(); 592 593 // add layer as a child to scene 594 scene->addChild(layer); 595 596 // return the scene 597 return scene; 598 } 599 600 // on "init" you need to initialize your instance 601 bool HelloWorld::init() 602 { 603 ////////////////////////////// 604 // 1. super init first 605 if ( !Layer::init() ) 606 { 607 return false; 608 } 609 610 Size visibleSize = Director::getInstance()->getVisibleSize(); 611 Vec2 origin = Director::getInstance()->getVisibleOrigin(); 612 613 auto board = Chessboard::create(); 614 addChild(board); 615 616 ///////////////////////////// 617 // 2. add a menu item with "X" image, which is clicked to quit the program 618 // you may modify it. 619 620 // add a "close" icon to exit the progress. it‘s an autorelease object 621 auto closeItem = MenuItemImage::create( 622 "CloseNormal.png", 623 "CloseSelected.png", 624 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 625 626 closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 627 origin.y + closeItem->getContentSize().height/2)); 628 629 // create menu, it‘s an autorelease object 630 auto menu = Menu::create(closeItem, NULL); 631 menu->setPosition(Vec2::ZERO); 632 this->addChild(menu, 1); 633 634 return true; 635 } 636 637 638 void HelloWorld::menuCloseCallback(Ref* pSender) 639 { 640 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) 641 MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 642 return; 643 #endif 644 645 Director::getInstance()->end(); 646 647 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 648 exit(0); 649 #endif 650 }
另外屏幕适配的地方,直接修改appDelegate.cpp
最后,附上效果图 ^^!
标签:style blog http io color ar os for sp
原文地址:http://www.cnblogs.com/jianghaitaoo/p/4071927.html