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

发布时间:2020-07-18 15:33:45 作者:蓬莱仙羽
来源:网络 阅读:1086

有一段时间不摸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


推荐阅读:
  1. MHA工具的优缺点归纳总结
  2. 宝鲲财经:三大外汇交易大忌

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

欢乐斗地主 ocos2d

上一篇:Nginx基础学习之如何使用realip模块

下一篇:Python中flask框架端口失效怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》