大佬教程收集整理的这篇文章主要介绍了Cocos2d-X3.0版的HelloWorld工程分析,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_262_1@@H_262_1@打开上一篇博客中的HelloWorld工程后,会看到下图所示的工程文件
@H_262_1@@H_262_1@main.cpp文件中的代码(本人已经注释)
@H_262_1@#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(); }
@H_262_1@main.cpp中的代码只是实现了下面的操作
1、定义一个App对象
5、执行App对象进入帧循环
注释:其中程序中真正重要的是最后一行代码中的run函数,run函数在后面的游戏开发中起到了至关重要的作用
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 / 60); //调用场景 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(); }@H_262_1@AppDelegate.cpp中的代码主要实现了游戏启动后执行的操作,游戏启动后的操作:
1、初始化导演类
2、获取以前创建的OpenGL视图
3、如果没有获取到OpenGL视图,重新创建OpenGL视图
4、设置openGL视图
5、设置是否显示调试信息
5、设置动画的帧数
6、调用场景(游戏真正的开始)
7、执行场景
@H_262_1@HelloWorldScene.h中的代码(本人已经注释)
@H_262_1@
#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
@H_262_1@@H_262_1@HelloWorldScene.cpp中的代码(本人已经注释)
@H_262_1@#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/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); 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(0); #endif }
@H_262_1@HelloWorldScene.cpp中的代码中的CCScene* HelloWorld::scene(),实现了创建场景的过程:
1、创建场景
2、创建层
3、将层加到场景上
4、返回场景
@H_262_1@HelloWorldScene.cpp中的代码中的CCScene* HelloWorld::init(),实现了初始化实例:
@H_262_1@1、初始化父类的Layer
@H_262_1@2、得到窗口的大小
@H_262_1@3、得到窗口的坐标
@H_262_1@4、创建菜单项
5、设置菜单项的位置
@H_262_1@6、设置菜单的位置
@H_262_1@7、将菜单加到层中
@H_262_1@8、创建标签
@H_262_1@9、设置标签的位置
@H_262_1@10、将标签加到层上
@H_262_1@11、创建精灵
@H_262_1@12、设置精灵的位置
@H_262_1@13、将精灵加到层上@H_262_1@
以上是大佬教程为你收集整理的Cocos2d-X3.0版的HelloWorld工程分析全部内容,希望文章能够帮你解决Cocos2d-X3.0版的HelloWorld工程分析所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。