thieumao

HelloWorld with Cocos2d-x

Mar 18th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3.  
  4. USING_NS_CC;
  5.  
  6. Scene* HelloWorld::createScene()
  7. {
  8.     // 'scene' is an autorelease object
  9.     auto scene = Scene::create();
  10.    
  11.     // 'layer' is an autorelease object
  12.     auto layer = HelloWorld::create();
  13.  
  14.     // add layer as a child to scene
  15.     scene->addChild(layer);
  16.  
  17.     // return the scene
  18.     return scene;
  19. }
  20.  
  21. // on "init" you need to initialize your instance
  22. bool HelloWorld::init()
  23. {
  24.     //////////////////////////////
  25.     // 1. super init first
  26.     if ( !Layer::init() )
  27.     {
  28.         return false;
  29.     }
  30.    
  31.     auto visibleSize = Director::getInstance()->getVisibleSize();
  32.     Vec2 origin = Director::getInstance()->getVisibleOrigin();
  33.  
  34.     /////////////////////////////
  35.     // 2. add a menu item with "X" image, which is clicked to quit the program
  36.     //    you may modify it.
  37.  
  38.     // add a "close" icon to exit the progress. it's an autorelease object
  39.     auto closeItem = MenuItemImage::create(
  40.                                            "CloseNormal.png",
  41.                                            "CloseSelected.png",
  42.                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
  43.    
  44.     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
  45.                                 origin.y + closeItem->getContentSize().height/2));
  46.  
  47.     // create menu, it's an autorelease object
  48.     auto menu = Menu::create(closeItem, NULL);
  49.     menu->setPosition(Vec2::ZERO);
  50.     this->addChild(menu, 1);
  51.  
  52.     /////////////////////////////
  53.     // 3. add your codes below...
  54.  
  55.     // add a label shows "Hello World"
  56.     // create and initialize a label
  57.    
  58.     auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
  59.    
  60.     // position the label on the center of the screen
  61.     label->setPosition(Vec2(origin.x + visibleSize.width/2,
  62.                             origin.y + visibleSize.height - label->getContentSize().height));
  63.  
  64.     // add the label as a child to this layer
  65.     this->addChild(label, 1);
  66.  
  67.     // add "HelloWorld" splash screen"
  68.     auto sprite = Sprite::create("HelloWorld.png");
  69.  
  70.     // position the sprite on the center of the screen
  71.     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  72.  
  73.     // add the sprite as a child to this layer
  74.     this->addChild(sprite, 0);
  75.    
  76.     return true;
  77. }
  78.  
  79.  
  80. void HelloWorld::menuCloseCallback(Ref* pSender)
  81. {
  82.     //Close the cocos2d-x game scene and quit the application
  83.     Director::getInstance()->end();
  84.  
  85.     #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  86.     exit(0);
  87.     #endif
  88.    
  89.     /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
  90.    
  91.     //EventCustom customEndEvent("game_scene_close_event");
  92.     //_eventDispatcher->dispatchEvent(&customEndEvent);
  93.    
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment