加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:58:09 所属栏目:百科 来源:网络整理
导读:打开上一篇博客中的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" "AppDelegate.h" "cocos2d.h" //命名空间 USING_NS_CC; //Cocos2d

打开上一篇博客中的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"
"AppDelegate.h"
"cocos2d.h"
//命名空间
USING_NS_CC;
//Cocos2d-X的主函数(相当于C/C++中的main函数)
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
nCmdShow)
{
//表示lpCmdLine、nCmdShow是两个没用的参数
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//定义一个app对象
AppDelegate app;
//执行app对象的run函数。进入帧循环
return Application::getInstance()->run();
}

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

1、定义一个App对象

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

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

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

?
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
"AppDelegate.h"
"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);
return true ;
}
//当程序进入后台后调用的函数(当在玩游戏时忽然别人打来电话时,程序进入后台)
void AppDelegate::applicationDidEnterBackground() {
//停止播放动画
Director::getInstance()->stopAnimation();
//暂停播放背景音乐
//SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
//当程序重新被激活的时候调用的函数(声音重新响起)
AppDelegate::applicationWillEnterForeground() {
//播放动画
Director::getInstance()->startAnimation();
//继续播放背景音乐
//SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
AppDelegate.cpp中的代码主要实现了游戏启动后执行的操作,游戏启动后的操作:
1、初始化导演类

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

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

4、设置openGL视图

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

5、设置动画的帧数

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

7、执行场景

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

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
"cocos2d.h"
//HelloWorld类继承自Layer类
class HelloWorld : public cocos2d::Layer
{
:
//创建场景
static cocos2d::Scene* createScene();
//初始化层
virtual bool init();
//菜单响应函数
menuCloseCallback(cocos2d::Ref* pSender);
//用于创建:场景、菜单、层等东西
CREATE_FUNC(HelloWorld);
};
#endif

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

?
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"HelloWorldScene.h"
//命名空间
USING_NS_CC;
//创建场景
Scene* HelloWorld::createScene()
{
//创建场景
auto scene = Scene::create();
//创建层
auto layer = HelloWorld::create();
//将层添加到场景中
scene->addChild(layer);
//返回场景
scene;
}
//初始化层
bool HelloWorld::init()
{
//初始化父类的Layer
(!Layer::init())
{
false ;
}
//获得窗口的大小
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/ ));
//创建菜单
auto menu = Menu::create(closeItem,NULL);
//设置菜单的坐标原点为左下角(菜单中默认的坐标原点在窗口的中央)
menu->setPosition(Vec2::ZERO);
//将菜单项添加到菜单中
->addChild(menu, 1 );
//创建一个标签
//第一个参数:标签中的内容
//第二个参数:字体
//第三个参数:字体大小
auto label = LabelTTF::create( "Hello World" "Arial" 24 );
//设置标签的位置
label->setPosition(Vec2(origin.x + visibleSize.width/ origin.y + visibleSize.height - label->getContentSize().height));
//设置标签的位置
->addChild(label,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">);
//创建一个精灵
auto sprite = Sprite::create( "HelloWorld.png" );
//设置精灵的位置
sprite->setPosition(Vec2(visibleSize.width/ + origin.x,visibleSize.height/ + origin.y));
//将精灵添加到层中
->addChild(sprite,0)!important; background:none!important">0 );
;
}
//菜单响应函数
HelloWorld::menuCloseCallback(Ref* pSender)
{
# (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" );
;
#endif
//结束场景
Director::getInstance()->end();
(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit( );
#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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读