您好,登錄后才能下訂單哦!
有一段時間不摸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; }
//創建線程 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。