cocos2dx 定义一个场景Scene

在 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

 

编译结果

 

转载于:https://my.oschina.net/dsc1025/blog/809562

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值