创建自定义的Cocos2d-x场景

本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3564988.html



操作步骤#

1、创建cocos2d-x工程

image

2、新建 Scene1.cpp Scene1.h

Scene1.h代码#

#ifndef __SCENE1_H__
#define __SCENE1_H__

#include "cocos2d.h"

#include "Box2D/Box2D.h"

#include "SimpleAudioEngine.h"

class Scene1 : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually 手动执行的"静态 node()"方法
CREATE_FUNC(Scene1);
};

#endif // __HELLOWORLD_SCENE_H__

Scene1.cpp代码#

#include "Scene1.h"

using namespace cocos2d;

CCScene* Scene1::scene()
{
//新建场景类实例
CCScene *scene = CCScene::create();

//定义布景层
Scene1 *layer = Scene1::create();

//将布景层加入场景
scene->addChild(layer);

//返回场景类
return scene;
}

//初始化函数
bool Scene1::init(){
return true;
}

修改 AppDelegate.cpp#

#include "cocos2d.h"
#include "CCEGLView.h"
#include "AppDelegate.h"
#include "Scene1.h"
#include "SimpleAudioEngine.h"

using namespace CocosDenshion;

USING_NS_CC;
//构造函数
AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
SimpleAudioEngine::end();
}

//程序启动时调用
bool AppDelegate::applicationDidFinishLaunching()
{
// 初始化导演类
CCDirector *pDirector = CCDirector::sharedDirector();
//设置OpenGL视图
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

//设置是否显示每帧时间
pDirector->setDisplayStats(true);

// 设置每帧时间 ,默认值 1.0/60
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object 创建开始场景
CCScene *pScene=Scene1::scene();

// run 运行场景
pDirector->runWithScene(pScene);
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()
{
CCDirector::sharedDirector()->stopAnimation();

SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

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

SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

运行#

6、右键 重新生成项目 然后 运行项目
 

运行成功#

WJKLB4OOG[J3C_)X)O5]0PB


 
作者:赵青青   一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
posted @   赵青青  阅读(686)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 易语言 —— 开山篇
· Trae初体验
CONTENTS
点击右上角即可分享
微信分享提示