【Cocos2d入门教程三】HelloWorld之一目了然

时间:2022-11-17 21:01:03

什么程序都是从HelloWorld先开始。同样Cocos2d-x我们先从HelloWorld进行下手、下面是HelloWorld的运行完成图:

【Cocos2d入门教程三】HelloWorld之一目了然

建立好的Cocos游戏项目中会有两个比较常用接触的文件夹。分别为Classes与resource。Classes存取代码文件,resource存取资源文件,下面为完整的项目架构:

【Cocos2d入门教程三】HelloWorld之一目了然

我们先来看下最基本的AppDelegate.cpp类

 #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 / ); //调用场景
auto scene = HelloWorld::createScene(); //执行场景
director->runWithScene(scene); return true;
} //当程序进入后台后调用的函数(当在玩游戏时忽然别人打来电话时,程序进入后台)
void AppDelegate::applicationDidEnterBackground() { //停止播放动画
Director::getInstance()->stopAnimation(); //暂停播放背景音乐
//SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
} //当程序重新被激活的时候调用的函数(声音重新响起)
void AppDelegate::applicationWillEnterForeground() { //播放动画
Director::getInstance()->startAnimation(); //继续播放背景音乐
//SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
} HelloWorld.h文件 #ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" //HelloWorld类继承自Layer类
class HelloWorld : public cocos2d::Layer
{
public:
//创建场景
static cocos2d::Scene* createScene(); //初始化层
virtual bool init(); //菜单响应函数
void menuCloseCallback(cocos2d::Ref* pSender); //用于创建:场景、菜单、层等东西
CREATE_FUNC(HelloWorld);
}; #endif
HelloWorldScene.cpp中的代码中的Scene* HelloWorld::scene(),实现了创建场景的过程::

1、创建场景

2、创建层

3、将层加到场景上

4、返回场景

HelloWorld.cpp

 #include "HelloWorldScene.h"

 //命名空间
USING_NS_CC; //创建场景
Scene* HelloWorld::createScene()
{
//创建场景
auto scene = Scene::create(); //创建层
auto layer = HelloWorld::create(); //将层添加到场景中
scene->addChild(layer); //返回场景
return scene;
} //初始化层
bool HelloWorld::init()
{
//初始化父类的Layer
if(!Layer::init())
{
return 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/ ,
origin.y + closeItem->getContentSize().height/)); //创建菜单
auto menu = Menu::create(closeItem, NULL); //设置菜单的坐标原点为左下角(菜单中默认的坐标原点在窗口的*)
menu->setPosition(Vec2::ZERO); //将菜单项添加到菜单中
this->addChild(menu, ); //创建一个标签
//第一个参数:标签中的内容
//第二个参数:字体
//第三个参数:字体大小
auto label = LabelTTF::create("Hello World", "Arial", ); //设置标签的位置
label->setPosition(Vec2(origin.x + visibleSize.width/,
origin.y + visibleSize.height - label->getContentSize().height)); //设置标签的位置
this->addChild(label, ); //创建一个精灵
auto sprite = Sprite::create("HelloWorld.png"); //设置精灵的位置
sprite->setPosition(Vec2(visibleSize.width/ + origin.x, visibleSize.height/ + origin.y)); //将精灵添加到层中
this->addChild(sprite, ); return true;
} //菜单响应函数
void HelloWorld::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();
#endif
}

HelloWorldScene.cpp中的 HelloWorld::init(),实现了初始化:

1、初始化父类的Layer

2、得到窗口的大小

3、得到窗口的坐标

4、创建菜单项

5、设置菜单项的位置

6、设置菜单的位置

7、将菜单加到层中

8、创建标签

9、设置标签的位置

10、将标签加到层上

11、创建精灵

12、设置精灵的位置

13、将精灵加到层上

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

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

定义一个App对象->执行App对象进入帧循环

一个游戏程序就这样执行起来,应运而生,是不是感觉特别的神奇。ok关于helloworld的解析就分享至此。下一章进入菜单篇的学习