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

来源:互联网 发布:上海的美食知乎 编辑:程序博客网 时间:2024/05/17 02:24

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



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



我们先来看下最基本的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();}

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/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}

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的解析就分享至此。下一章进入菜单篇的学习




1 0