cocos2d-x入门三 分层设计框架

helloworld就是一个完整的框架,大致分为四个层次如下:

    导演-------场景------图层-----精灵

Director-----Scene----Layer----Sprite

  1. 导演类负责的是全局工作,关键代码主要由系统自动生成。在类AppDelegate中,它主要由三个函数组成,
    bool AppDelegate::applicationDidFinishLaunching() 入口函数
    void AppDelegate::applicationDidEnterBackground() 当前游戏由运行态转入后台运行
    void AppDelegate::applicationWillEnterForeground() 游戏由后台转到前台
    导演类常见的接口有, 
    auto director = Director::getInstance(); 初始化,获得导演实例

    auto glview = director->getOpenGLView();

    director->setOpenGLView(glview);

    关联OPENGL

    director->setDisplayStats(true);

    director->setAnimationInterval(1.0 / 60);

    设置FPS,正常值的范围[30,60]

    director->runWithScene(scene);

    director->replaceWithScene(scene);

    设置当前运行的场景

    设置当场景变化时,要显示的新场景

    Director::getInstance()->stopAnimation();

    Director::getInstance()->startAnimation();

    当有其它任务过来时,要暂停游戏

    返回游戏时,重新开始

    pop push 2.0中场景是以栈的形式存储
  2. 场景类一般比较简单,一个导演类可对应多个场景。其实现主要在HelloWorldScene.cpp中,常见的只有初始化,auto scene = Scene::create();,其中不管哪个类的create函数,都会new一个对象,而系统会自动通过语句 ret->autorelease();把它加入到自动释放列表中。
  3. 图层类,实现主要在HelloWorldScene.cpp中,初始化auto layer = HelloWorld::create();通过scene->addChild(layer);把场景与图层关联起来,一个场景可有多个图层。
  4. 精灵类,图层的具体内容又由精灵填充,实现主要在HelloWorldScene.cpp中的bool HelloWorld::init()中,初始化auto sprite = Sprite::create("HelloWorld.png"); 精灵与其它组件都在这个函数中初始化,并设置相应的坐标,最后通过 this->addChild(sprite, 0);语句加到图层上,第二个参数表示在图层中的前后位置,0是背景。
posted @ 2015-10-12 16:05  知奇所以然  阅读(701)  评论(0编辑  收藏  举报