Cocos2D-X学习笔记 2 创建一个场景

AppDelegate 继承  CCApplication, CCApplication 继承 CCApplicationProtocol


CCApplicationProtocol是个接口,里面都是写纯虚函数

    virtual bool applicationDidFinishLaunching() = 0;
    virtual void applicationDidEnterBackground() = 0;
    virtual void applicationWillEnterForeground() = 0;
    virtual void setAnimationInterval(double interval) = 0;
    virtual ccLanguageType getCurrentLanguage() = 0;
    virtual TargetPlatform getTargetPlatform() = 0;


AppDelegate里实现了前三个函数

    virtual bool applicationDidFinishLaunching();
    virtual void applicationDidEnterBackground();
    virtual void applicationWillEnterForeground();

CCApplication 里实现了后三个函数

    void setAnimationInterval(double interval);

    virtual ccLanguageType getCurrentLanguage();

    virtual TargetPlatform getTargetPlatform();


在main函数里调用CCApplication::sharedApplication()->run();就调用了

CCDirector::sharedDirector()->mainLoop();起定时器循环调用drawScene();


bool AppDelegate::applicationDidFinishLaunching()里创建我们自己的场景前有两个准备工作

1、总导演上场

CCDirector *pDirector = CCDirector::sharedDirector();

pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

2、选定胶片和放映频率

pDirector->setDisplayStats(true);

pDirector->setAnimationInterval(1.0 / 60);


创建自己的场景,调用系统默认的default.png

StartLayer *sl = new StartLayer();
sl->init();
sl->autorelease();

cocos2d::CCScene *s = cocos2d::CCScene::create();
s->addChild(sl);
    // run
pDirector->runWithScene(s);
    return true;


StartLayer.h

#pragma once


#include "cocos2d.h"
class StartLayer : public cocos2d::CCLayer
{
public:
StartLayer();
virtual ~StartLayer();
bool init();
};


StartLayer.cpp

#include "StartLayer.h"
StartLayer::StartLayer(void)
{
}

StartLayer::~StartLayer(void)
{
}
bool StartLayer::init()
{
if(!CCLayer::init())
{
return false;
}
return true;
}










你可能感兴趣的:(cocos2d,cocos2d-x)