温馨提示×

温馨提示×

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

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

[cocos2d-x]HelloWorldDemo

发布时间:2020-07-30 11:27:07 来源:网络 阅读:281 作者:蓬莱仙羽 栏目:游戏开发

实现一个demo,具备以下功能:

1.让几个字分别位于中间和四个角落。

2.中间的字体改变,并且带有闪烁功能。

3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触摸设置为true。

4.下面两个按钮能够实现添加节点和移除节点的作用。

效果图:

[cocos2d-x]HelloWorldDemo

实现代码:

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); }


向AI问一下细节

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

AI