cocos2dx一个场景增添多个层

cocos2dx一个场景增添多个层,第1张

概述给一个场景添加两个层吧。 首先创建两个layer,以下是头文件 #pragma once#include "cocos2d.h"USING_NS_CC;class BackgroundLayer : public cocos2d::CCLayer{public: virtual bool init(); CREATE_FUNC(BackgroundLayer);}; #pragma

给一个场景添加两个层吧。

首先创建两个layer,以下是头文件

#pragma once#include "cocos2d.h"USING_NS_CC;class BackgroundLayer : public cocos2d::cclayer{public:	virtual bool init();	CREATE_FUNC(BackgroundLayer);};

#pragma once#include "cocos2d.h"USING_NS_CC;class FishLayer : public cocos2d::cclayer{public:	virtual bool init();	CREATE_FUNC(FishLayer);};

现在去源文件实现一下:

#include "BackgroundLayer.h"bool BackgroundLayer::init(){ CCSprite* bg = CCSprite::create("bg.png"); bg->setposition(ccp(visiblesize.wIDth/2,visiblesize.height/2)); addChild(bg); return true;} #include "FishLayer.h"bool FishLayer::init(){ CCSprite* bg = CCSprite::create("bgfish.png"); bg->setposition(ccp(visiblesize.wIDth/2,204)">
ok,现在已经有了两个层了,建一个scene并添加他们:
#pragma once#include "cocos2d.h"#include "FishLayer.h"#include "BackgroundLayer.h"class GameScene : public cocos2d::CCScene{public: virtual bool init(); static CCScene* playGame(); };
去源文件添加啦:
#include "GameScene.h"CCScene* GameScene::playGame(){ GameScene* scene = new GameScene(); scene->init(); return scene;}bool GameScene::init(){ BackgroundLayer* pLayer_bg = BackgroundLayer::create(); FishLayer* pLayer_fish = FishLayer::create(); addChild(pLayer_bg); addChild(pLayer_fish); return true;} ok,现在场景里面已经有两个层了,可以分别在这两个层里添加东西了。 总结

以上是内存溢出为你收集整理的cocos2dx一个场景增添多个层全部内容,希望文章能够帮你解决cocos2dx一个场景增添多个层所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1009747.html

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

发表评论

登录后才能评论

评论列表(0条)

保存