Cocos2d-x之helloworld

前端之家收集整理的这篇文章主要介绍了Cocos2d-x之helloworld前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Cocos2d-x之helloworld


AppDelegate.h

  1. </pre><pre name="code" class="cpp">#ifndef _APP_DELEGATE_H_
  2. #define _APP_DELEGATE_H_
  3.  
  4. #include "cocos2d.h"
  5.  
  6. /**
  7. @brief The cocos2d Application.
  8.  
  9. The reason for implement as private inheritance is to hide some interface call by Director.
  10. */
  11. class AppDelegate : private cocos2d::Application
  12. {
  13. public:
  14. AppDelegate();
  15. virtual ~AppDelegate();
  16.  
  17. virtual void initGLContextAttrs();
  18.  
  19. /**
  20. @brief Implement Director and Scene init code here.
  21. @return true Initialize success,app continue.
  22. @return false Initialize Failed,app terminate.
  23. */
  24. virtual bool applicationDidFinishLaunching();
  25.  
  26. /**
  27. @brief The function be called when the application enter background
  28. @param the pointer of the application
  29. */
  30. virtual void applicationDidEnterBackground();
  31.  
  32. /**
  33. @brief The function be called when the application enter foreground
  34. @param the pointer of the application
  35. */
  36. virtual void applicationWillEnterForeground();
  37. };
  38.  
  39. #endif // _APP_DELEGATE_H_


AppDelegate.cpp

  1. #include "AppDelegate.h"
  2. #include "HelloWorldScene.h"
  3.  
  4. USING_NS_CC;
  5. //是Cocosd-x提供的一个宏,用来代替using namespace cocos2d语句
  6.  
  7. AppDelegate::AppDelegate() {
  8.  
  9. }
  10.  
  11. AppDelegate::~AppDelegate()
  12. {
  13. }
  14.  
  15. //if you want a different context,just modify the value of glContextAttrs
  16. //it will takes effect on all platforms
  17. void AppDelegate::initGLContextAttrs()
  18. {
  19. //set OpenGL context attributions,now can only set six attributions:
  20. //red,green,blue,alpha,depth,stencil
  21. GLContextAttrs glContextAttrs = {8,8,24,8};
  22.  
  23. GLView::setGLContextAttrs(glContextAttrs);
  24. }
  25.  
  26. //游戏进入后台调用函数
  27. bool AppDelegate::applicationDidFinishLaunching() {
  28. // initialize director
  29. auto director = Director::getInstance();
  30. auto glview = director->getOpenGLView(); //设置导演类的OpenGL视图
  31. if(!glview) {
  32. glview = GLViewImpl::create("My Game");
  33. director->setOpenGLView(glview);
  34. }
  35.  
  36. // turn on display FPS
  37. director->setDisplayStats(true);//设置在屏幕上是否显示帧率等信息
  38.  
  39. // set FPS. the default value is 1.0/60 if you don't call this
  40. director->setAnimationInterval(1.0 / 60);//设置定时器1.0/60秒间隔一次
  41.  
  42. // create a scene. it's an autorelease object
  43. auto scene = HelloWorld::createScene();//设置创建场景对象Scene
  44.  
  45. // run
  46. director->runWithScene(scene);//运行该场景
  47.  
  48. return true;
  49. }
  50.  
  51. //游戏进入后台调用函数
  52. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  53. void AppDelegate::applicationDidEnterBackground() {
  54. Director::getInstance()->stopAnimation();//停止场景中的动画
  55.  
  56. // if you use SimpleAudioEngine,it must be pause
  57. // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); //停止背景音乐
  58. }
  59.  
  60. //游戏进入前台调用函数
  61. // this function will be called when the app is active again
  62. void AppDelegate::applicationWillEnterForeground() {
  63. Director::getInstance()->startAnimation();//开始场景中的动画
  64.  
  65. // if you use SimpleAudioEngine,it must resume here
  66. // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();//继续背景音乐
  67. }


HelloWorldScene.h

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. class HelloWorld : public cocos2d::Layer
  7. {
  8. public:
  9. // there's no 'id' in cpp,so we recommend returning the class instance pointer
  10. static cocos2d::Scene* createScene();
  11.  
  12. // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
  13. virtual bool init();
  14. // a selector callback
  15. void menuCloseCallback(cocos2d::Ref* pSender);
  16. // implement the "static create()" method manually
  17. CREATE_FUNC(HelloWorld);
  18. };
  19.  
  20. #endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp

  1. #include "HelloWorldScene.h"
  2.  
  3. USING_NS_CC;
  4.  
  5. Scene* HelloWorld::createScene()
  6. {
  7. // 'scene' is an autorelease object
  8. auto scene = Scene::create();
  9. // 'layer' is an autorelease object
  10. auto layer = HelloWorld::create();
  11.  
  12. // add layer as a child to scene
  13. scene->addChild(layer);
  14.  
  15. // return the scene
  16. return scene;
  17. }
  18.  
  19. // on "init" you need to initialize your instance
  20. bool HelloWorld::init()
  21. {
  22. //////////////////////////////
  23. // 1. super init first
  24. if ( !Layer::init() )
  25. {
  26. return false;
  27. }
  28. Size visibleSize = Director::getInstance()->getVisibleSize();
  29. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  30.  
  31. /////////////////////////////
  32. // 2. add a menu item with "X" image,which is clicked to quit the program
  33. // you may modify it.
  34.  
  35. // add a "close" icon to exit the progress. it's an autorelease object
  36. auto closeItem = MenuItemImage::create(
  37. "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
  38. closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));
  39.  
  40. // create menu,it's an autorelease object
  41. auto menu = Menu::create(closeItem,NULL);
  42. menu->setPosition(Vec2::ZERO);
  43. this->addChild(menu,1);
  44.  
  45. /////////////////////////////
  46. // 3. add your codes below...
  47.  
  48. // add a label shows "Hello World"
  49. // create and initialize a label
  50. auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.ttf",24);
  51. // position the label on the center of the screen
  52. label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));
  53.  
  54. // add the label as a child to this layer
  55. this->addChild(label,1);
  56.  
  57. // add "HelloWorld" splash screen"
  58. auto sprite = Sprite::create("HelloWorld.png");
  59.  
  60. // position the sprite on the center of the screen
  61. sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
  62.  
  63. // add the sprite as a child to this layer
  64. this->addChild(sprite,0);
  65. return true;
  66. }
  67.  
  68.  
  69. void HelloWorld::menuCloseCallback(Ref* pSender)
  70. {
  71. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  72. MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  73. return;
  74. #endif
  75.  
  76. Director::getInstance()->end();
  77.  
  78. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  79. exit(0);
  80. #endif
  81. }

猜你在找的Cocos2d-x相关文章