Cocos2d-x tutorial 1 hello world

Source: Internet
Author: User

HelloworldScene. h # ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H _ # include "cocos2d. h "class HelloWorld: public cocos2d: CCLayer {public: // initialization function virtual bool init (); // static creation function static cocos2d: CCScene * scene (); // a callback function void menuCloseCallback (CCObject * pSender) in the menu; // Note 1 CREATE_FUNC (HelloWorld);}; # endif // _ HELLOWORLD_SCENE_H _ HelloWorldScene. cpp # include "HelloWorldScene. h "# incl Ude "SimpleAudioEngine. h "using namespace cocos2d; using namespace CocosDenshion; CCScene * HelloWorld: scene () {// create a scenario object CCScene * scene = CCScene: create (); // create a HelloWorld object (Layer layer) HelloWorld * layer = HelloWorld: create (); // put the child who uses the Layer as the scenario into the scenario scene-> addChild (layer ); return scene;} // on "init" you need to initialize your instancebool HelloWorld: init () {// call the parent class initialization function first if (! CCLayer: init () {return false;} // create an "X" image as an image menu item, // bind the callback function after the user clicks to the CCMenuItemImage * pCloseItem = callback: create ("CloseNormal.png", "CloseSelected.png", this, menu_selector (HelloWorld :: menuCloseCallback); // set the position of the menu item pCloseItem-> setPosition (ccp (CCDirector: sharedDirector ()-> getWinSize (). width-20, 20); // Add the created Menu item to the Menu to generate a Menu object instance CCMenu * pMenu = CCMenu: create (pCloseItem, NULL ); // set the menu as CCPointZero: x = 0, y = 0 pMenu-> setPosition (CCPointZero); // Add the menu to the current Layer, and set the Z axis to 1 this-> addChild (pMenu, 1); // create a Text object CCLabelTTF * pLabel = CCLabelTTF: create ("Hello World", "Thonburi ", 34); // get the current device size, CCSize size = CCDirector: sharedDirector ()-> getWinSize (); // set the text coordinate pLabel-> setPosition (ccp (size. width/2, size. height-20); // Add the text to the current Layer this-> addChild (pLabel, 1); // use the HelloWorld.png image to create a Sprite * pSprite = CCSprite :: create ("HelloWorld.png"); // set the sprite position pSprite-> setPosition (ccp (size. width/2, size. height/2); // place the genie in the current layer this-> addChild (pSprite, 0); return true;} voidHelloWorld: menuCloseCallback (CCObject * pSender) {// exit CCDirector: shareddire()-> end (); // determine whether the current device is an ios device # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif}

Related Article

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.