实现一个demo,具备以下功能:
1.让几个字分别位于中间和四个角落。
2.中间的字体改变,并且带有闪烁功能。
3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触摸设置为true。
4.下面两个按钮能够实现添加节点和移除节点的作用。
效果图:
实现代码:
HelloWorldScene.h:
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); void menuRemoveCallback(CCObject* pSender); //启动触屏事件 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //触摸注册事件 virtual void registerWithTouchDispatcher(); //单点触摸事件 virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); //移动事件 virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent); }; #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp:
#include "HelloWorldScene.h" #include "SimpleAudioEngine.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; } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } //设置当前允许触摸 this->setTouchEnabled(true); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2- 30, 20) ); CCMenuItemImage *pCloseItem1 = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuRemoveCallback) ); pCloseItem1->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width / 2 + 30, 20) ); CCMenu* pMenu = CCMenu::create(pCloseItem1,pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); CCLabelTTF* pLabel = CCLabelTTF::create("江苏理工", "Thonburi", 34); CCSize size = CCDirector::sharedDirector()->getWinSize(); //一开始设置为绿色 pLabel->setColor(ccGREEN); pLabel->setPosition( ccp(size.width / 2, size.height / 2) ); this->addChild(pLabel,1); //让节点闪烁的方法 CCAction *action = CCBlink::create(5, 20); pLabel->runAction(action); //变色的方法 CCAction *action1 = CCTintTo::create(5, 255, 0, 0); pLabel->runAction(action1); //左上角显示姓名 CCLabelTTF* pLabel1 = CCLabelTTF::create("丁小未", "Thonburi", 34); CCSize size1 = CCDirector::sharedDirector()->getWinSize(); pLabel1->setAnchorPoint(ccp(0, 1)); pLabel1->setPosition( ccp(0, size1.height) ); this->addChild(pLabel1,1); //右上角显示性别 CCLabelTTF* pLabel2 = CCLabelTTF::create("男", "Thonburi", 34); CCSize size2 = CCDirector::sharedDirector()->getWinSize(); pLabel2->setAnchorPoint(ccp(1, 1)); pLabel2->setPosition( ccp(size2.width, size2.height) ); this->addChild(pLabel2,1); //右下角显示年龄 CCLabelTTF* pLabel3 = CCLabelTTF::create("23", "Thonburi", 34); CCSize size3 = CCDirector::sharedDirector()->getWinSize(); pLabel3->setAnchorPoint(ccp(1, 0)); pLabel3->setPosition( ccp(size3.width, 0) ); this->addChild(pLabel3,1); return true; } void HelloWorld::menuCloseCallback(CCObject* pSender) { //结束关闭事件 // CCDirector::sharedDirector()->end(); // //#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // exit(0); //#endif CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *pLabel = CCLabelTTF::create("我是添加的", "Thonburi", 24); pLabel->setPosition(ccp(size.width/2+30,size.height/2+30)); pLabel->setTag(10); this->addChild(pLabel,1); } void HelloWorld::menuRemoveCallback(CCObject *pSender) { CCNode *pLabel = this->getChildByTag(10); this->removeChild(pLabel); } //多点触摸方法 void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { //添加子视图 //随机数是CCRANDOM_0_1,是产生0-1之间的随机数 // CCSize size = CCDirector::sharedDirector()->getWinSize(); // CCLabelTTF *pLabel = CCLabelTTF::create("触屏添加", "Thonburi", 24); // pLabel->setPosition(ccp(100, 100)); // pLabel->setTag(10); // this->addChild(pLabel,1); CCLog("多点触摸Began"); } bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("单点触摸"); return true;//如果这个不返回true的话,则move方法没用 } void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCLog("单点moved"); } //触摸注册事件 //如果没有这个,默认的是多点触摸,Targeted是单点,Standed是多点触摸 void HelloWorld::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); }
搞了一段时间的unity3d,最近又重温一下cocos2dx,由于没有mac机,就只能用windows来配置开发环境,依旧从HelloWorld开始!
配置环境:我用的是VS2012,python2.7.3貌似,安装完毕后设置一下环境变量,添加上C:\Python27,然后cocos2dx2.4.2版本,然后cmd创建新工程。
然后在project文件夹下就能看到新建的工程了!
效果图:
上面红色的字我已经标注的很清楚,新手第一次碰到的时候都会想,我要输入中文,但显示出来确实乱码,这就是编码问题,只要转码就OK了!
代码:
CCLabelTTF *labelWelcome0=CCLabelTTF::create("Hello","微软雅黑",48); CCLabelTTF *labelWelcome1=CCLabelTTF::create("World","宋体",48); CCLabelTTF *labelWelcome2=CCLabelTTF::create("Welcome","Consolas",48); CCLabelTTF *labelWelcome3=CCLabelTTF::create("DingXiaowei","Bitstream Vera Sans Mono",48); CCSize size = CCDirector::sharedDirector()->getWinSize(); float width = size.width; float height = size.height; CCSize s0 = labelWelcome0->getContentSize(); CCSize s1 = labelWelcome1->getContentSize(); CCSize s2 = labelWelcome2->getContentSize(); CCSize s3 = labelWelcome3->getContentSize(); labelWelcome0->setPosition(CCPointMake(s0.width/2,size.height-s0.height/2)); labelWelcome1->setPosition(ccp(size.width-s1.width/2,size.height-s1.height/2)); labelWelcome2->setPosition(CCPoint(s2.width/2,s2.height/2)); labelWelcome3->setPosition(ccp(size.width-s3.width/2,s3.height/2)); addChild(labelWelcome0,1); addChild(labelWelcome1,1); addChild(labelWelcome2,1); addChild(labelWelcome3,1); CCLabelTTF * namelabel = CCLabelTTF::create(G2U("Cocos2d-x,我爱你!"),"微软雅黑",48); namelabel->setPosition(ccp(size.width/2,size.height/2)); addChild(namelabel,1);
转码的方法:
设置字体颜色:
//转码成utf-8,windows默认gb2312 char* HelloWorld::G2U(const char* gb2312) { int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0); wchar_t* wstr = new wchar_t[len+1]; memset(wstr, 0, len+1); MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len); len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); char* str = new char[len+1]; memset(str, 0, len+1); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); if(wstr) delete[] wstr; return str; }
设置字体颜色:
//设置颜色,其实是一个rgb三原色,0-255的三种组合 srand((unsigned) time(NULL)); ccColor3B c0,c1,c2,c3,c4; c0.r=rand()%256; c0.g=rand()%256; c0.b=rand()%256; c1.r=rand()%256; c1.g=rand()%256; c1.b=rand()%256; c2.r=rand()%256; c2.g=rand()%256; c2.b=rand()%256; c3.r=rand()%256; c3.g=rand()%256; c3.b=rand()%256; c4.r=rand()%256; c4.g=rand()%256; c4.b=rand()%256; labelWelcome0->setColor(c0); labelWelcome1->setColor(c1); labelWelcome2->setColor(c2); labelWelcome3->setColor(c3);