Cocos2d-x 3.0 创建一个场景,并设置现场的时候,项目开始执行上主动

#ifndef __TEST_H__
#define __TEST_H__


#include "cocos2d.h"
USING_NS_CC;


class Test : public Layer
{
public: //不要忘记public不然默觉得private的不能正常执行
static Scene * createScene();
virtual bool init();
CREATE_FUNC(Test);
};
#endif //__TEST_H__


源文件

#include "Test.h"
USING_NS_CC;


Scene * Test::createScene()
{
auto scene = Scene::create();
auto layer = Test::create();
scene->addChild(layer);
return scene;
}
bool Test::init()
{
if (!Layer::init())
{
return false;
}


Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();


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);
return true;
}

然后在AppDelegate.cpp中。加入头文件


#include "AppDelegate.h"
#include "HelloWorldScene.h"


#include "Test.h"  //加入头文件


USING_NS_CC;


AppDelegate::AppDelegate() {


}


AppDelegate::~AppDelegate() 
{
}


bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
        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);


    // create a scene. it's an autorelease object
    //auto scene = HelloWorld::createScene();
auto scene = Test::createScene();  //实例化场景


    // run
    director->runWithScene(scene); //把实例化的场景名加进来就能够了


    return true;
}


// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();


    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}


// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();


    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}


























原文地址:https://www.cnblogs.com/lcchuguo/p/4586329.html