自己动手写cocos2dx游戏引擎(三)——Director对象

前端之家收集整理的这篇文章主要介绍了自己动手写cocos2dx游戏引擎(三)——Director对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上一节中,最后调用到了APPDelegate中的applicationDidFinishLaunching方法

在applicationDidFinishLaunching方法中,获取到了Director的一个实例。

Director返回的是其子类DisplayLinkDirector的对象,其实现如下:

class Director {
    public:
        virtual ~Director() {}
        static Director* getInstance();
        virtual bool init();
};

class DisplayLinkDirector : public Director {
    public:

};

static DisplayLinkDirector *s_SharedDirector = NULL;

Director* Director::getInstance()
{
    if (!s_SharedDirector) {
        s_SharedDirector = new DisplayLinkDirector;
        s_SharedDirector->init();
    }   
    return s_SharedDirector;
}

bool Director::init()
{
    return true;
}

在Director的getInstance中,返回了一个静态的子类对象,并调用了其初始化方法


在AppDelegate中调用

#ifndef __APP_DELEGATE__
#define __APP_DELEGATE__

#include "Application.h"
#include "Director.h"

#include <iostream>

class AppDelegate: private Application {
    public:
        virtual bool applicationDidFinishLaunching() {
            std::cout << "delegate applicationDidFinishLaunching" << std::endl; 
            Director* director = Director::getInstance();
            return true;
        }   
};

#endif

添加了两个类Director和其子类DisplayLinkDirector。


附代码:demo3

原文链接:https://www.f2er.com/cocos2dx/340571.html

猜你在找的Cocos2d-x相关文章