COCOS2D-X HelloWorld项目的启动流程

COCOS2D-X HelloWorld项目的启动流程,第1张

概述项目创建完毕后。见Class目录。 主要是AppDelegate(应用导演)和HelloWorldScene(示例层)。 AppDelegate 是 Cocos2D-x的内部入口。 从 *** 作系统到Cocos 会先进入 AppDelegate。 也就是当 '引擎' 初始化完毕后,会调用AppDelegate的 ApplicationDidFinishLaunching 方法。 在 AppDelegat 项目创建完毕后。见Class目录。

主要是AppDelegate(应用导演)和HelloWorldScene(示例层)。

AppDelegate 是 Cocos2D-x的内部入口。

从 *** 作系统到Cocos 会先进入 AppDelegate。

也就是当 '引擎' 初始化完毕后,会调用AppDelegate的 ApplicationDIDFinishLaunching 方法。

在 AppDelegate 中 执行游戏的初始化,设置分辨率并启动场景。






[AppDelegate.cpp]

bool AppDelegate::applicationDIDFinishLaunching() {    // initialize director    auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    //初始化OpenGLs视图 如果成功就会根据对应平台完成对象实现。    if(!glvIEw) {#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_liNUX)        glvIEw = GLVIEwImpl::createWithRect("SmartGame",Rect(0,designResolutionSize.wIDth,designResolutionSize.height));#else        glvIEw = GLVIEwImpl::create("SmartGame");#endif    //导演了设置OpenGL        director->setopenGLVIEw(glvIEw);    }    // turn on display FPS (显示帧率)    director->setdisplayStats(true);    // set FPS. the default value is 1.0/60 if you don't call this    // 一般肉眼这个刷新频率够了    director->setAnimationInterval(1.0 / 60);    // Set the design resolution    // 设置  屏幕分辨率    glvIEw->setDesignResolutionSize(designResolutionSize.wIDth,designResolutionSize.height,ResolutionPolicy::NO_border);    Size frameSize = glvIEw->getFrameSize();    //如果OpenGl拿出的高度大于中等大小才会执行的代码块,应该是适屏设置。    if (frameSize.height > mediumResolutionSize.height)    {                director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height,largeResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is larger than the height of small size.    else if (frameSize.height > smallResolutionSize.height)    {                director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height,mediumResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is smaller than the height of medium size.    else    {                director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height,smallResolutionSize.wIDth/designResolutionSize.wIDth));    }    register_all_packages();    // create a scene. it's an autorelease object    // 创建HelloWorld场景,这个场景对象会交给Cocosed-x管理,不需要手动释放    auto scene = HelloWorld::createScene();    // 运行这个场景    director->runWithScene(scene);    return true;}





当导演runWithScene 运行该场景时。

会先进入该场景的ini 初始化方法。


[HelloWorldScene.cpp]


bool HelloWorld::init(){    //////////////////////////////    // 1. super init first,必须先执行父类的初始化。    if ( !Layer::init() )    {        return false;    }    // 屏幕的可见区域值    Size visibleSize = Director::getInstance()->getVisibleSize();    // 原点的位置    Vec2 origin = Director::getInstance()->getVisibleOrigin();    // 关闭按钮是一个 MenuItemImage (菜单项) 对象。    // create 方法中的前两个参数代表着选中与非选中状态显示的图。    // 当他被单击的时候,CC_CALLBACK_1开始执行回调函数,即helloWorld的menuCloseCallback    // 函数原型    /**                                *参数 Ref* 代表单击事件都发送者,即MenuItemImage!        voID HelloWorld::menuCloseCallback(Ref* sender){           Director::getInstance()->end();        }    */    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));    // create menu,it's an autorelease object    auto menu = Menu::create(closeItem,NulL);    // 设置类菜单位置,以后记住这代表右下角。    menu->setposition(Vec2::ZERO);    // 此行代表加到场景中。    // 第二个参数代表绘制层的顺序,默认参数值为0,    // 表示最高优先层导入,该值越大表示该层在最后加载(在最高一层),一般背景图层是首先加载,    // 其他元素在加载在背景层上面。因为背景在后面加载的话,会覆盖掉前面加载的元素,看不到想要的图层组合效果。    this->addChild(menu,1);    // 创建文本,选择指定字体库,设置字号    auto label = Label::createWithTTF("Hello World","Fonts/Marker Felt.ttf",24);    // 设置位置,X 剧中,y 偏上    // 此处标记 Vec2的居中布局位置 : (原点x轴的起始位置向右偏移至总屏幕可见范围一半的位置    // 即 origin.x + visibleSize.wIDth/2    label->setposition(Vec2(origin.x + visibleSize.wIDth/2,origin.y + visibleSize.height - label->getContentSize().height));    // add the label as a child to this layer    this->addChild(label,1);    // add "HelloWorld" splash screen"    // 这里创建里一个精灵,用于显示纹理(就是贴图)。    auto sprite = Sprite::create("HelloWorld.png");    // x y 轴起点偏移至屏幕可见范围的一半,上下左右都居中    sprite->setposition(Vec2(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));    // add the sprite as a child to this layer    this->addChild(sprite,0);        return true;}



本文内容源自: http://spu.dangdang.com/23947634.html 总结

以上是内存溢出为你收集整理的COCOS2D-X HelloWorld项目的启动流程全部内容,希望文章能够帮你解决COCOS2D-X HelloWorld项目的启动流程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1084621.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存