[Cocos2d-x] HelloWorldDemo

Source: Internet
Author: User

Implement a demo with the following functions: 1. Place the words in the middle and four corners. 2. The font in the middle is changed and the flash function is enabled. 3. Single-point touch and multi-point touch, and can achieve the sliding effect, if sliding, you must use a single-point touch with the return value of bool to set to true. 4. The following two buttons can be used to add and remove nodes. : Implementation code: 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 se Lector callback void menuCloseCallback (CCObject * pSender); // preprocessor macro for "static create ()" constructor (node () deprecated) CREATE_FUNC (HelloWorld ); void menuRemoveCallback (CCObject * pSender); // start the touch screen event virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent); // touch registration event virtual void register (); // single-touch event virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent ); // Mobile event virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) ;};# endif // _ HELLOWORLD_SCENE_H _ HelloWorldScene. cpp: [plain] view plaincopyprint? # Include "HelloWorldScene. h "# include" SimpleAudioEngine. h "using namespace cocos2d; using namespace CocosDenshion; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} bool HelloW Orld: init () {if (! CCLayer: init () {return false;} // you can set this-> setTouchEnabled (true); CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("CloseNormal.png ", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback); pCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width/2-30, 20); optional * 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 ("Jiangsu Emy of technology", "Thonburi", 34); CCSize size = CCDirector: shareddire () -> getWinSize (); // set it to green pLabel-> setColor (ccGREEN) at the beginning; pLabel-> setPosition (ccp (size. width/2, size. height/2); this-> addChild (pLabel, 1); // CCAction * action = CCBlink: create (5, 20 ); pLabel-> runAction (action); // color change method CCAction * action1 = CCTintTo: create (5,255, 0, 0); pLabel-> runAction (action1 ); // The name CCLabelTTF * pLabel1 = CCLabelTTF: create ("ding Xiaowei", "Thonburi", 34) is displayed in the upper left corner. CCSize size1 = CCDirector: shareddire () -> getWinSize (); pLabel1-> setAnchorPoint (ccp (0, 1); pLabel1-> setPosition (ccp (0, size1.height); this-> addChild (pLabel1, 1); // the upper right corner shows the gender CCLabelTTF * pLabel2 = CCLabelTTF: create ("male", "Thonburi", 34); CCSize size2 = CCDirector: shareddire () -> getWinSize (); pLabel2-> setAnchorPoint (ccp (1, 1); pLabel2-> setPosition (ccp (size2.width, size2.height); this-> addChild (pLabel2, 1); // The lower-right corner shows the age of CCLabelTTF * pLabel3 = CCLabelTTF: create ("23", "Thonburi", 34); CCSize size3 = CCDirector: shareddire () -> getWinSize (); pLabel3-> setAnchorPoint (ccp (1, 0); pLabel3-> setPosition (ccp (size3.width, 0); this-> addChild (pLabel3, 1); return true;} void HelloWorld: menuCloseCallback (CCObject * pSender) {// close the event // CCDirector: shareddire()-> end (); //// # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) // exit (0); // # endif CCSize size = CCDirector: sharedDirector ()-> getWinSize (); CCLabelTTF * pLabel = CCLabelTTF: create ("I added", "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) {// Add a subview // the random number is CCRANDOM_0_1, which is a random number between 0 and 1. // CCSize size = CCDirector: shareddire () -> getWinSize (); // CCLabelTTF * pLabel = CCLabelTTF: create ("add touch screen", "Thonburi", 24 ); // pLabel-> setPosition (ccp (100,100); // pLabel-> setTag (10); // this-> addChild (pLabel, 1 ); CCLog ("multi-touch Began");} bool HelloWorld: ccTouchBegan (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent) {CCLog ("single point of Touch "); return true; // if true is not returned, the move method is useless.} void HelloWorld: ccTouchMoved (cocos2d: CCTouch * pTouch, cocos2d: CCEvent * pEvent) {CCLog ("Single Point moved");} // click it to register an event. // if no event exists, the default value is multi-point touch, Targeted is single point, and Standed is multi-point touch void HelloWorld :: registerWithTouchDispatcher () {CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.