Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3. #include "NewScene.h"
  4.  
  5. USING_NS_CC;
  6. //using namespace CocosDenshion;
  7.  
  8. Scene* HelloWorld::createScene()
  9. {
  10.     // 'scene' is an autorelease object
  11.     auto scene = Scene::create();
  12.  
  13.     // 'layer' is an autorelease object
  14.     auto layer = HelloWorld::create();
  15.  
  16.     // add layer as a child to scene
  17.     scene->addChild(layer);
  18.  
  19.     // return the scene
  20.     return scene;
  21. }
  22.  
  23. // on "init" you need to initialize your instance
  24. bool HelloWorld::init()
  25. {
  26.     //////////////////////////////
  27.     // 1. super init first
  28.     if ( !Layer::init() )
  29.     {
  30.         return false;
  31.     }
  32.  
  33.     auto visibleSize = Director::getInstance()->getVisibleSize();
  34.     Vec2 origin = Director::getInstance()->getVisibleOrigin();
  35.  
  36.     /////////////////////////////
  37.     // 2. add a menu item with "X" image, which is clicked to quit the program
  38.     //    you may modify it.
  39.  
  40.     // add a "close" icon to exit the progress. it's an autorelease object
  41.     auto closeItem = MenuItemImage::create(
  42.                                            "CloseNormal.png",
  43.                                            "CloseSelected.png",
  44.                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
  45.  
  46.     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
  47.                                 origin.y + closeItem->getContentSize().height/2));
  48.  
  49.      auto gambar = Sprite::create("icon-114.png");
  50.      gambar -> setPosition(Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  51.      auto action = RotateBy::create(0.5,360);
  52.      this -> addChild(gambar,2);
  53.      auto repet = RepeatForever::create(action);
  54.      gambar -> runAction(repet);
  55.      auto gerak = MoveBy::create(1, Vec2(20,0));
  56.      auto gerak1 = MoveBy::create(0.5, Vec2(-10,0));
  57.      auto gerak2 = MoveTo::create(3, Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  58.      auto seq = Sequence::create(gerak,gerak1,gerak2,NULL);
  59.      gambar -> runAction(seq);
  60.  
  61.  
  62.    auto listener = EventListenerTouchOneByOne::create();
  63.     listener -> setSwallowTouches(true);
  64.      listener -> onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
  65.      //listener -> onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
  66.      //listener -> onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
  67.  
  68.     // _eventDispatcher -> addEventListenerWithSceneGraphPriority(listener, this);
  69.  
  70.     // create menu, it's an autorelease object
  71.     auto menu = Menu::create(closeItem, NULL);
  72.     menu->setPosition(Vec2::ZERO);
  73.     this->addChild(menu, 1);
  74.  
  75.     /////////////////////////////
  76.     // 3. add your codes below...
  77.  
  78.     // add a label shows "Hello World"
  79.     // create and initialize a label
  80.  
  81.     auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
  82.  
  83.     // position the label on the center of the screen
  84.     label->setPosition(Vec2(origin.x + visibleSize.width/2,
  85.                             origin.y + visibleSize.height - label->getContentSize().height));
  86.  
  87.     // add the label as a child to this layer
  88.     this->addChild(label, 1);
  89.  
  90.     // add "HelloWorld" splash screen"
  91.     auto sprite = Sprite::create("HelloWorld.png");
  92.  
  93.     // position the sprite on the center of the screen
  94.     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  95.  
  96.     // add the sprite as a child to this layer
  97.     this->addChild(sprite, 0);
  98.  
  99.     //auto audio = CocosDenshion::SimpleAudioEngine::sharedEngine();
  100.     //audio -> preloadBackgroundMusic("notify.ogg");
  101.     //audio -> playBackgroundMusic("notify.ogg", true);
  102.  
  103.      CocosDenshion::SimpleAudioEngine::getInstance() -> preloadEffect("lagu/klop.ogg");
  104.      CocosDenshion::SimpleAudioEngine::getInstance() -> playEffect("lagu/klop.ogg", true);
  105.     // CocosDenshion::SimpleAudioEngine::getInstance() -> setBackgroundMusicVolume(0.2);
  106.  
  107.      auto menu1 = MenuItemFont::create("Play", CC_CALLBACK_1(HelloWorld::play, this));
  108.      auto menu2 = MenuItemFont::create("High Score", CC_CALLBACK_1(HelloWorld::highscore, this));
  109.      auto menu3 = MenuItemFont::create("Setting", CC_CALLBACK_1(HelloWorld::setting, this));
  110.      auto menu4 = MenuItemImage::create("ABU.png","COKLAT.png",CC_CALLBACK_1(HelloWorld::imagebutton, this));
  111.  
  112.      menu1 -> setPosition(Vec2(visibleSize.width /2 , (visibleSize.height/4) * 3));
  113.     menu2 -> setPosition(Vec2(visibleSize.width /2 , (visibleSize.height/4) * 1));
  114.     menu3 -> setPosition(Vec2(visibleSize.width /2 , (visibleSize.height/4) * 2));
  115.     menu4 -> setPosition(Vec2( origin.x + (visibleSize.width /4) , visibleSize.height/2));
  116.  
  117.      auto *menu22 = Menu::create(menu1, menu2, menu3, NULL);
  118.      menu22 -> setPosition(Vec2(0,0));
  119.      this -> addChild(menu22,2);
  120.  
  121.           auto *menu222 = Menu::create(menu4, NULL);
  122.           menu222 -> setPosition(Vec2(0,0));
  123.           this -> addChild(menu222,2);
  124.  
  125.     return true;
  126. }
  127.  
  128.      void HelloWorld::imagebutton(cocos2d::Ref *pSender)
  129.      {
  130.         CCLOG("gambar");
  131.      }
  132.  
  133.      void HelloWorld::play(cocos2d::Ref *pSender)
  134.      {
  135.        CCLOG("play");
  136.        auto scene = NewScene::createScene();
  137.        Director::getInstance() -> pushScene(scene);
  138.      }
  139.  
  140.      void HelloWorld::highscore(cocos2d::Ref *pSender)
  141.           {
  142.              CCLOG("highscore");
  143.           }
  144.  
  145.      void HelloWorld::setting(cocos2d::Ref *pSender)
  146.           {
  147.              CCLOG("setting");
  148.           }
  149.  
  150.      bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
  151.      {
  152.         CCLOG("AAAAA");
  153.         return true;
  154.     }
  155.  
  156.     //void HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
  157.     ///{
  158.    //      auto gerak1 = MoveBy::create(0.5, Vec2(-10,0));
  159.    //      auto gerak2 = MoveTo::create(3, Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  160.    //           auto gambar = Sprite::create("icon-114.png");
  161.    //           gambar -> setPosition(Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  162.    //     gambar -> runAction(gerak1);
  163.  
  164. //    }
  165.  
  166.     // void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
  167.      //{
  168.         //  auto gerak1 = MoveBy::create(0.5, Vec2(-10,0));
  169.          // auto gerak2 = MoveTo::create(3, Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  170.         //       auto gambar = Sprite::create("icon-114.png");
  171.        //        gambar -> setPosition(Point((visibleSize.width / 2) + origin.x , (visibleSize.height / 2) + origin.y));
  172.     //    gambar -> runAction(gerak2);
  173.     // }
  174.  
  175. void HelloWorld::menuCloseCallback(Ref* pSender)
  176. {
  177.     //Close the cocos2d-x game scene and quit the application
  178.     Director::getInstance()->end();
  179.  
  180.     #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  181.     exit(0);
  182. #endif
  183.  
  184.     /*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*/
  185.  
  186.     //EventCustom customEndEvent("game_scene_close_event");
  187.     //_eventDispatcher->dispatchEvent(&customEndEvent);
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement