在 src 文件夹下,添加 GameScene.cpp 和 GameScene.h 两个文件
参照HelloWorldScene.h
编写 GanmeScene.h头文件
#ifndef __GAME_SCENE_H__
#define __GAME_SCENE_H__
#include "cocos2d.h"
class GameScene : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(GameScene);
};
#endif // ___GAME_SCENE_H__
编写 GameScene.cpp
#include "GameScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* GameScene::createScene()
{
auto scene = Scene::create();
auto layer = GameScene::create();
scene->addChild(layer);
return scene;
}
bool GameScene::init() {
if (!Layer::init())
{
return false;
}
...
}
编写完成
场景布置完成,必须告诉 director,
打开 AppDelegate.cpp 引入编写好的 GameScene.h 文件
#include "GameScene.h"
...
bool AppDelegate::applicationDidFinishLaunching() {
...
// create a scene. it's an autorelease object
auto scene = GameScene::createScene();
// run
director->runWithScene(scene);
...
return true;
}
官方文档对 runWithScene 描述
void runWithScene(Scene * scene)
Enters the Director's main loop with the given Scene.
Call it to run only your FIRST scene. Don't call it if there is already a running scene.
It will call pushScene: and then it will call startAnimation
编译结果