创建自定义的Cocos2d-x场景

创建自定义的Cocos2d-x场景,第1张

概述 *** 作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#define __SCENE1_H__#in *** 作步骤

1、创建cocos2d-x工程

2、新建 Scene1.cpp Scene1.h

Scene1.h代码
#ifndef __SCENE1_H__
#define __SCENE1_H__

#include "cocos2d.h"

"Box2D/Box2D.h"

"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代码
"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
"cocos2d.h"
"CCEGLVIEw.h"
"AppDelegate.h"
"Scene1.h"
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、右键 重新生成项目 然后 运行项目 运行成功


  总结

以上是内存溢出为你收集整理的创建自定义的Cocos2d-x场景全部内容,希望文章能够帮你解决创建自定义的Cocos2d-x场景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/998925.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存