中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[cocos2dx]斗地主制作之界面搭建

發布時間:2020-07-18 15:33:45 來源:網絡 閱讀:1077 作者:蓬萊仙羽 欄目:開發技術

有一段時間不摸cocos2dx了,今兒想重新溫馨一下,不然以前學的就荒廢了,想還是做個斗地主吧,網絡版的,服務器端以后再寫,今天就先搭個界面!

效果圖:

[cocos2dx]斗地主制作之界面搭建
搭建的界面Code:
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include <netdb.h> #include <pthread.h>  using namespace cocos2d; using namespace CocosDenshion;  CCScene* HelloWorld::scene() {     // 'scene' is an autorelease object     CCScene *scene = CCScene::create();          // 'layer' is an autorelease object     HelloWorld *layer = HelloWorld::create();      // add layer as a child to scene     scene->addChild(layer);      // return the scene     return scene; }  int socketHandle = 0; //創建BSD連接 //ip and port int HelloWorld::connect(const char *ip, unsigned short port) {     struct sockaddr_in sa;     struct hostent * hp;          hp = gethostbyname(ip);     if (!hp) {         return -1;     }          memset(&sa,0,sizeof(sa));     memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length);     sa.sin_family=hp->h_addrtype;     sa.sin_port = HTONS(port);          socketHandle = socket(sa.sin_family, SOCK_STREAM, 0);     if(socketHandle<0)     {         printf("failed to create socket\n");         return -1;     }     if (::connect(socketHandle, (sockaddr *)&sa, sizeof(sa))) {         printf("failed to connect socket\n");         return -1;     }          CCLog("Client connect OK! IP:%s:%d",ip,port);     return 0; }  //創建線程 int HelloWorld::threadStart() {     int errCode = 0;     do {         pthread_attr_t tAttr;                  errCode = pthread_attr_init(&tAttr);                  CC_BREAK_IF(errCode!=0);                  errCode = pthread_attr_setdetachstate(&tAttr, PTHREAD_CREATE_DETACHED);         if (errCode!=0) {             pthread_attr_destroy(&tAttr);             break;         }                  //errCode = pthread_create(&threadHimi, &tAttr, thread_function, this);     } while (0);     return errCode; }  // on "init" you need to initialize your instance bool HelloWorld::init() {     //////////////////////////////     // 1. super init first     if ( !CCLayer::init() )     {         return false;     }      //    char * ip = "192.168.1.24"; //    unsigned short port = 5005; //     //    int con = HelloWorld::connect(ip, port); //    if (con>0) { //        printf("error"); //    } //    else //    { //         //        printf("登陸成功"); //    }          // ask director the window size     CCSize size = CCDirector::sharedDirector()->getWinSize();               CCSprite * bg = CCSprite::create("bg.jpg");     bg->setAnchorPoint(CCPointMake(0, 1));     bg->setPosition(CCPointMake(0, size.height));     bg->setScaleX(0.58);     this->addChild(bg);          CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("card.plist");     CCSpriteBatchNode * batchNode = CCSpriteBatchNode::create("card.pvr.ccz");     batchNode->setPosition(CCPointZero);     this->addChild(batchNode);          for (int i=1; i<=13; i++)     {         CCString *name = CCString::createWithFormat("Hong%d.png",i);         CCSprite*card1=CCSprite::createWithSpriteFrameName(name->getCString());         card1->setScale(0.7);         card1->setPosition(CCPointMake(140 + i*15,size.height/2-95));         batchNode->addChild(card1);     }          CCSprite * role = CCSprite::createWithSpriteFrameName("role.png");          role->setScale(0.6);     role->setPosition(CCPointMake(60, 80));     batchNode->addChild(role);          CCSprite * role1 = CCSprite::createWithSpriteFrameName("role1.png");     role1->setScale(0.5);     role1->setPosition(CCPointMake(60, 220));     batchNode->addChild(role1);          CCSprite * role2 = CCSprite::createWithSpriteFrameName("role1.png");     role2->setScale(0.5);     role2->setPosition(CCPointMake(420,220));     batchNode->addChild(role2);               //左邊牌     for (int i=2; i<=13; i++)     {         CCSprite*card1=CCSprite::createWithSpriteFrameName("SmallCardBack1.png");         card1->setScale(0.6);         card1->setRotation(-82);         card1->setPosition(CCPointMake(115+i ,size.height/2-13+i*11));         batchNode->addChild(card1);     }               //右邊牌     for (int i=2; i<=13; i++)     {         CCSprite*card1=CCSprite::createWithSpriteFrameName("SmallCardBack1.png");         card1->setScale(0.6);         card1->setRotation(82);         card1->setPosition(CCPointMake(365-i,size.height/2-13+i*11));         batchNode->addChild(card1);     }               CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                         "CloseNormal.png",                                         "CloseSelected.png",                                         this,                                         menu_selector(HelloWorld::menuCloseCallback) );     pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width/2, size.height-20) );     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);     pMenu->setPosition( CCPointZero );     this->addChild(pMenu, 1);           bgMusic = true;     return true; }  void HelloWorld::menuCloseCallback(CCObject* pSender) { //    CCDirector::sharedDirector()->end(); // //#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) //    exit(0); //#endif     bgMusic=!bgMusic;     if (bgMusic) {         SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();     }     else     {         //播放場景音樂         SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.mp3", true);     }      } 



下面關于Socket和多線程是為了以后做網絡端做準備的,就先貼出來,有興趣的自己研究,關于這方面的可以參考Himi的博客:http://blog.csdn.net/xiaominghimi/article/details/7603123
關于Cocos2dx連接到服務器端的Code:
int socketHandle = 0; //創建BSD連接 //ip and port int HelloWorld::connect(const char *ip, unsigned short port) {     struct sockaddr_in sa;     struct hostent * hp;          hp = gethostbyname(ip);     if (!hp) {         return -1;     }          memset(&sa,0,sizeof(sa));     memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length);     sa.sin_family=hp->h_addrtype;     sa.sin_port = HTONS(port);          socketHandle = socket(sa.sin_family, SOCK_STREAM, 0);     if(socketHandle<0)     {         printf("failed to create socket\n");         return -1;     }     if (::connect(socketHandle, (sockaddr *)&sa, sizeof(sa))) {         printf("failed to connect socket\n");         return -1;     }          CCLog("Client connect OK! IP:%s:%d",ip,port);     return 0; }

多線程的Code:
//創建線程 int HelloWorld::threadStart() {     int errCode = 0;     do {         pthread_attr_t tAttr;                  errCode = pthread_attr_init(&tAttr);                  CC_BREAK_IF(errCode!=0);                  errCode = pthread_attr_setdetachstate(&tAttr, PTHREAD_CREATE_DETACHED);         if (errCode!=0) {             pthread_attr_destroy(&tAttr);             break;         }                  //errCode = pthread_create(&threadHimi, &tAttr, thread_function, this);     } while (0);     return errCode; }



上面是非常簡答的搭了個界面的代碼,但還調了一段時間,后期繼續跟新邏輯代碼!


==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學習,共同進步 ===================

 

轉載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/17636601

歡迎關注我的微博:http://weibo.com/u/2590571922


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黔江区| 江孜县| 兴和县| 将乐县| 绵竹市| 吉林省| 佛坪县| 玉环县| 天等县| 吉木萨尔县| 阳春市| 乌鲁木齐市| 衡山县| 日喀则市| 营口市| 禹州市| 察哈| 翁牛特旗| 阿尔山市| 大余县| 古田县| 宾阳县| 青岛市| 常山县| 宁安市| 托克托县| 大埔区| 特克斯县| 托里县| 邵阳县| 镇平县| 五寨县| 庆城县| 常宁市| 镇坪县| 阳信县| 吴堡县| 桐庐县| 玉溪市| 如皋市| 光泽县|