有一段时间不摸cocos2dx了,今儿想重新温馨一下,不然以前学的就荒废了,想还是做个斗地主吧,网络版的,服务器端以后再写,今天就先搭个界面!
效果图:#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); } }
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