Cocos2d-X3.0版的HelloWorld工程分析

来源:互联网 发布:网络编辑是做什么的 编辑:程序博客网 时间:2024/06/14 14:58

打开上一篇博客中的HelloWorld工程后,会看到下图所示的工程文件

\


main.cpp文件中的代码(本人已经注释)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include"main.h"
#include"AppDelegate.h"
#include"cocos2d.h"
 
//命名空间
USING_NS_CC;
 
//Cocos2d-X的主函数(相当于C/C++中的main函数) 
intAPIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int      nCmdShow)
{
    //表示lpCmdLine、nCmdShow是两个没用的参数 
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
 
     //定义一个app对象 
    AppDelegate app;
 
    //执行app对象的run函数。进入帧循环 
    returnApplication::getInstance()->run();
}

main.cpp中的代码只是实现了下面的操作

1、定义一个App对象

5、执行App对象进入帧循环

注释:其中程序中真正重要的是最后一行代码中的run函数,run函数在后面的游戏开发中起到了至关重要的作用

AppDelegate.cpp文件中的代码(本人已经注释了)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include"AppDelegate.h"
#include"HelloWorldScene.h"
 
 
//命名空间
USING_NS_CC;
 
 
//构造函数
AppDelegate::AppDelegate() {
 
}
 
 
//析构函数
AppDelegate::~AppDelegate()
{
}
 
 
//程序启动完成后会进入的函数 
bool AppDelegate::applicationDidFinishLaunching() {
     
    //初始化导演
    auto director = Director::getInstance();
 
    //获得OpenGL视图
    auto glview = director->getOpenGLView();
 
    //如果没有获取OpenGL视图
    if(!glview)
    {
        //创建OpenGL视图
        glview = GLView::create("My Game");
 
        //设置OpenGL视图
        director->setOpenGLView(glview);
    }
 
    //设置是否显示调试信息
    director->setDisplayStats(true);
 
    //设置帧率
    director->setAnimationInterval(1.0/ 60);
 
    //调用场景
    auto scene = HelloWorld::createScene();
 
    //执行场景
    director->runWithScene(scene);
 
    returntrue;
}
 
 
//当程序进入后台后调用的函数(当在玩游戏时忽然别人打来电话时,程序进入后台) 
voidAppDelegate::applicationDidEnterBackground() {
    
    //停止播放动画
    Director::getInstance()->stopAnimation();
 
    //暂停播放背景音乐
    //SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
 
 
//当程序重新被激活的时候调用的函数(声音重新响起) 
voidAppDelegate::applicationWillEnterForeground() {
     
    //播放动画
    Director::getInstance()->startAnimation();
 
    //继续播放背景音乐
    //SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
AppDelegate.cpp中的代码主要实现了游戏启动后执行的操作,游戏启动后的操作:

1、初始化导演类

2、获取以前创建的OpenGL视图

3、如果没有获取到OpenGL视图,重新创建OpenGL视图

4、设置openGL视图

5、设置是否显示调试信息

5、设置动画的帧数

6、调用场景(游戏真正的开始)

7、执行场景


HelloWorldScene.h中的代码(本人已经注释)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
 
#include"cocos2d.h"
 
//HelloWorld类继承自Layer类
classHelloWorld : publiccocos2d::Layer
{
public:
    //创建场景
    staticcocos2d::Scene* createScene();
 
    //初始化层
    virtual bool init(); 
     
    //菜单响应函数
    voidmenuCloseCallback(cocos2d::Ref* pSender);
     
    //用于创建:场景、菜单、层等东西 
    CREATE_FUNC(HelloWorld);
};
 
#endif

HelloWorldScene.cpp中的代码(本人已经注释)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include"HelloWorldScene.h"
 
//命名空间
USING_NS_CC;
 
//创建场景
Scene* HelloWorld::createScene()
{
    //创建场景
    auto scene = Scene::create();
     
    //创建层
    auto layer = HelloWorld::create();
 
    //将层添加到场景中
    scene->addChild(layer);
 
    //返回场景
    returnscene;
}
 
//初始化层
bool HelloWorld::init()
{
    //初始化父类的Layer
    if(!Layer::init())
    {
        returnfalse;
    }
     
    //获得窗口的大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
 
    //获得坐标原点的坐标
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
 
     //用图片创建菜单项 
     //第一个参数:正常状态下的图片 
     //第二个参数:被选中时的图片 
     //第三个参数:响应函数 
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
     
    //设置菜单项的位置
    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,
                                origin.y + closeItem->getContentSize().height/2));
 
    //创建菜单
    auto menu = Menu::create(closeItem, NULL);
   
    //设置菜单的坐标原点为左下角(菜单中默认的坐标原点在窗口的中央) 
    menu->setPosition(Vec2::ZERO);
 
    //将菜单项添加到菜单中
    this->addChild(menu,1);
 
    //创建一个标签 
    //第一个参数:标签中的内容 
    //第二个参数:字体 
    //第三个参数:字体大小 
    auto label = LabelTTF::create("Hello World","Arial",24);
     
    //设置标签的位置
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
 
   //设置标签的位置
    this->addChild(label,1);
 
    //创建一个精灵
    auto sprite = Sprite::create("HelloWorld.png");
 
    //设置精灵的位置
    sprite->setPosition(Vec2(visibleSize.width/2+ origin.x, visibleSize.height/2+ origin.y));
 
    //将精灵添加到层中
    this->addChild(sprite,0);
     
    returntrue;
}
 
//菜单响应函数
voidHelloWorld::menuCloseCallback(Ref* pSender)
{
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif
 
    //结束场景
    Director::getInstance()->end();
 
#if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


HelloWorldScene.cpp中的代码中的CCScene* HelloWorld::scene(),实现了创建场景的过程:

1、创建场景

2、创建层

3、将层加到场景上

4、返回场景


HelloWorldScene.cpp中的代码中的CCScene* HelloWorld::init(),实现了初始化实例:

1、初始化父类的Layer

2、得到窗口的大小

3、得到窗口的坐标

4、创建菜单项

5、设置菜单项的位置

6、设置菜单的位置

7、将菜单加到层中

8、创建标签

9、设置标签的位置

10、将标签加到层上

11、创建精灵

12、设置精灵的位置

13、将精灵加到层上




FROM: http://www.2cto.com/kf/201501/370238.html

0 0
原创粉丝点击