zoukankan      html  css  js  c++  java
  • HelloWorld入门

    HelloWorld入门
        代码解释:
            AppDelegate.h类是Cocos2d-x引擎要求实现的游戏应用委托对象,在Cocos2d-x游戏
        运行的不同生命周期阶段会触发他的不同函数
            AppDelegate继承了cocos2d::Application,是Cocos2d-x引擎提供的基类

        AppDelegate.h
            

     1 #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     /**
    18     @brief    Implement Director and Scene init code here.
    19     @return true    Initialize success, app continue.
    20     @return false   Initialize failed, app terminate.
    21     */
    22     /*
    23         *游戏启动时调用的函数,在这里可以初始化导演对象和场景对象
    24     */
    25     virtual bool applicationDidFinishLaunching();
    26 
    27     /**
    28     @brief  The function be called when the application enter background
    29     @param  the pointer of the application
    30     */
    31     /*
    32         *游戏进入后台时调用的函数
    33     */
    34     virtual void applicationDidEnterBackground();
    35 
    36     /**
    37     @brief  The function be called when the application enter foreground
    38     @param  the pointer of the application
    39     */
    40     /*
    41         *游戏进入前台时调用的函数
    42     */
    43     virtual void applicationWillEnterForeground();
    44 };
    45 
    46 #endif // _APP_DELEGATE_H_



            
            AppDelegate.cpp

     1 #include "AppDelegate.h"
     2 #include "HelloWorldScene.h"
     3 
     4 USING_NS_CC;//是用来替换using namespace cocos2d
     5 
     6 AppDelegate::AppDelegate() {
     7 
     8 }
     9 
    10 AppDelegate::~AppDelegate()
    11 {
    12 }
    13 
    14 bool AppDelegate::applicationDidFinishLaunching() {
    15     // initialize director
    16     //初始化director
    17     auto director = Director::getInstance();
    18     auto glview = director->getOpenGLView();
    19     if (!glview) {
    20         glview = GLView::create("My Game");
    21         director->setOpenGLView(glview);//设置导演类的OpenGL视图
    22     }
    23 
    24     // turn on display FPS
    25     //设置是否在屏幕上显示帧率等信息,一般是为了测试,实际发布时会影响游戏的外观,所以就不显示了
    26     director->setDisplayStats(true);
    27 
    28     // set FPS. the default value is 1.0/60 if you don't call this
    29     //设定定时器1.0/60秒间隔一次,即设定帧率为60
    30     director->setAnimationInterval(1.0 / 60);
    31 
    32     // create a scene. it's an autorelease object
    33     //创建场景的对象Sence
    34     auto scene = HelloWorld::createScene();
    35 
    36     // run
    37     //运行该场景,使游戏进入该场景
    38     director->runWithScene(scene);
    39 
    40     return true;
    41 }
    42 
    43 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
    44 void AppDelegate::applicationDidEnterBackground() {
    45     //停止场景中的动画
    46     Director::getInstance()->stopAnimation();
    47 
    48     // if you use SimpleAudioEngine, it must be pause
    49     //停止背景音乐
    50     // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    51 }
    52 
    53 // this function will be called when the app is active again 
    54 void AppDelegate::applicationWillEnterForeground() {
    55     //开始游戏场景中的动画
    56     Director::getInstance()->startAnimation();
    57 
    58     // if you use SimpleAudioEngine, it must resume here
    59     //继续背景音乐
    60     // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    61 }



            HelloWorld类继承cocos2d::Layer类,他被成为层(layer),这些层被放到场景(scene)场景类是cocos2d::Scene
            
            HelloWorldScene.h
            

     1 #ifndef __HELLOWORLD_SCENE_H__
     2 #define __HELLOWORLD_SCENE_H__
     3 
     4 #include "cocos2d.h"
     5 
     6 class HelloWorld : public cocos2d::Layer//继承了Layer是一个层不是场景
     7 {
     8 public:
     9     // there's no 'id' in cpp, so we recommend returning the class instance pointer
    10     // 声明创建当前层HelloWorld所在场景的静态函数createScene()
    11     static cocos2d::Scene* createScene();
    12 
    13     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    14     virtual bool init();//初始化层的实例函数
    15     
    16     // a selector callback
    17     void menuCloseCallback(cocos2d::Ref* pSender);//声明菜单回掉函数,用于触摸菜单时间的回掉
    18     
    19     // implement the "static create()" method manually
    20     CREATE_FUNC(HelloWorld);//创建一个静态函数create,可以创建层
    21 };
    22 
    23 #endif // __HELLOWORLD_SCENE_H__



            
            
            HelloWorldScene.cpp

      1 #include "HelloWorldScene.h"
      2 
      3 USING_NS_CC;
      4 
      5 /*
      6     *做了三件事,首先创建HelloWorld层所在的场景对象
      7     *其次创建类HelloWorld层
      8     *最后将HelloWorld层添加到场景scene中
      9 */
     10 Scene* HelloWorld::createScene()
     11 {
     12     // 'scene' is an autorelease object
     13     auto scene = Scene::create();//创建HelloWorld层所在的场景对象
     14     
     15     // 'layer' is an autorelease object
     16     auto layer = HelloWorld::create();//创建类HelloWorld层
     17 
     18     // add layer as a child to scene
     19     scene->addChild(layer);//最后将HelloWorld层添加到场景scene中
     20 
     21     // return the scene
     22     return scene;
     23 }
     24 
     25 // on "init" you need to initialize your instance
     26 bool HelloWorld::init()
     27 {
     28     //////////////////////////////
     29     // 1. super init first
     30     //初始化父类Layer
     31     if ( !Layer::init() )
     32     {
     33         return false;
     34     }
     35     
     36     //定义视图的可视化尺寸
     37     Size visibleSize = Director::getInstance()->getVisibleSize();
     38     //定义视图的可视化原点
     39     Vec2 origin = Director::getInstance()->getVisibleOrigin();
     40 
     41     /////////////////////////////
     42     // 2. add a menu item with "X" image, which is clicked to quit the program
     43     //    you may modify it.
     44 
     45     // add a "close" icon to exit the progress. it's an autorelease object
     46     //增加一个菜单项,单击它的时候退出程序
     47     //创建一个图片菜单项对象,单击该菜单项的时候回掉menuCloseCallback函数
     48     auto closeItem = MenuItemImage::create(
     49                                            "CloseNormal.png",
     50                                            "CloseSelected.png",
     51                                            CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
     52     //菜单项的位置
     53     closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
     54                                 origin.y + closeItem->getContentSize().height/2));
     55 
     56     // create menu, it's an autorelease object
     57     //创建Menu菜单对象
     58     auto menu = Menu::create(closeItem, NULL);
     59     //定义菜单对象的位置
     60     menu->setPosition(Vec2::ZERO);
     61     //把菜单项对象添加到当前层上
     62     this->addChild(menu, 1);
     63 
     64     /////////////////////////////
     65     // 3. add your codes below...
     66 
     67     // add a label shows "Hello World"
     68     // create and initialize a label
     69     //在下面添加自己的代码
     70     //创建一个LabelTTF标签对象
     71     auto label = LabelTTF::create("Hello World", "Arial", 24);
     72     
     73     // position the label on the center of the screen
     74     //设置标签对象位置为水平居中,在垂直方向上与屏幕顶对齐
     75     label->setPosition(Vec2(origin.x + visibleSize.width/2,
     76                             origin.y + visibleSize.height - label->getContentSize().height));
     77 
     78     // add the label as a child to this layer
     79     //将文本对象添加到层中
     80     this->addChild(label, 1);
     81 
     82     // add "HelloWorld" splash screen"
     83     //创建精灵Sprite对象
     84     auto sprite = Sprite::create("HelloWorld.png");
     85 
     86     // position the sprite on the center of the screen
     87     //设置精灵对象的位置,是屏幕的中央
     88     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
     89 
     90     // add the sprite as a child to this layer
     91     //将精灵添加到层中
     92     this->addChild(sprite, 0);
     93     
     94     return true;
     95 
     96 }
     97 
     98 
     99 void HelloWorld::menuCloseCallback(Ref* pSender)
    100 {
    101     //CC_TARGET_PLATFORM读取当前运行的平台的宏
    102 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    103     MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    104     return;
    105 #endif
    106 
    107     Director::getInstance()->end();
    108 
    109 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    110     exit(0);
    111 #endif
    112 }
  • 相关阅读:
    如何更改VS2005调试网站的浏览器类型
    StringBuilder 的 Capacity属性
    Convert.ToInt32,Int32.Parse和Int32.TryParse的关系
    今天第一天注册
    关于Random产生随机数测试
    [导入]Reporting Services 4: Web Service
    [导入]Reporting Services 5: Extensions & Custom Report Item
    silverlight缓存无法更新的简易解决办法
    总结前段时间做的电话业务故障处理系统(1)
    atlas
  • 原文地址:https://www.cnblogs.com/dudu580231/p/4394848.html
Copyright © 2011-2022 走看看