本教程将学习如何使用多个CCLayer
接下来 先讲解怎么在一个CCScene中使用多个CCLayer
先写三个类Bottomlayer,UIlayer,MainScene ,代码如下:
MainScene :在本例中主要用于生成CCScene对象
Bottomlayer:在本例中本层可以实现独立的拖动,旋转
UIlayer:在本例中本曾保持禁止
- /*
- * MainScene.h
- *
- * Created on: 2012-9-13
- * Author: Panda
- */
- #ifndef MAINSCENE_H_
- #define MAINSCENE_H_
- #include "cocos2d.h"
- USING_NS_CC;
- typedef enum{
- TargetLayerMin=0,
- TargetLayerMain,
- TargetLayerGame,
- TargetLayerUI,
- TargetLayerMax,
- }Target;
- class Bottomlayer: public CCLayer {
- public:
- Bottomlayer();
- virtual ~Bottomlayer();
- bool init();
- CREATE_FUNC(Bottomlayer);
- };
- class UIlayer: public CCLayer {
- public:
- UIlayer();
- virtual ~UIlayer();
- bool init();
- CREATE_FUNC(UIlayer);
- private:
- };
- class MainScene: public CCLayer {
- public:
- MainScene();
- virtual ~MainScene();
- virtual bool init();
- static CCScene *scene();
- CREATE_FUNC(MainScene);
- };
- #endif /* MAINSCENE_H_ */
应该很简单就能看懂吧?感觉上面的代码也没啥好讲的,所以就略过了
技巧:这里要提一下使用枚举类型的好处。上述代码中的 Target 就是一个枚举类型。使用枚举类型以后你就不需要再去记忆哪个节点使用了哪个数字作为tag,你可以清楚的知道哪个节点使用了哪个枚举类型。实际上使用什么数字并不重要;重要的是同一个节点要统一使用同一个数字。同样的方法可以在动作(Action)tag 上使用。 |
- /*
- * MainScene.cpp
- *
- * Created on: 2012-9-13
- * Author: Panda
- */
- #include "MainScene.h"
- MainScene::MainScene() {
- // TODO Auto-generated constructor stub
- }
- MainScene::~MainScene() {
- // TODO Auto-generated destructor stub
- }
- bool MainScene::init() {
- bool bRet = false;
- do {
- bRet = true;
- } while (0);
- return bRet;
- }
- CCScene *MainScene::scene() {
- CCScene *pScene = NULL;
- do {
- pScene = CCScene::create();
- MainScene *main = MainScene::create();
- Bottomlayer *game = Bottomlayer::create();
- UIlayer *ui = UIlayer::create();
- //将main层添加到最下层
- pScene->addChild(main, 0, TargetLayerMain);
- //game层为中间层
- pScene->addChild(game, 1, TargetLayerGame);
- //ui为最上层
- pScene->addChild(ui, 2, TargetLayerUI);
- } while (0);
- return pScene;
- }
- Bottomlayer::Bottomlayer(){
- }
- Bottomlayer::~Bottomlayer(){
- }
- bool Bottomlayer::init() {
- bool bRet = false;
- do {
- //添加一个精灵到Bottom层,不多解释 没啥问题
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSprite *sprite = CCSprite::create("layer1.jpg");
- sprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(sprite);
- bRet = true;
- } while (0);
- return true;
- }
- /*-----------------------------UI层代码参考Bottom层--------------------------------------*/
- UIlayer::UIlayer(){
- }
- UIlayer::~UIlayer(){
- }
- bool UIlayer::init() {
- bool bRet = false;
- do {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSprite *sprite = CCSprite::create("layer2.jpg");
- sprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(sprite);
- bRet = true;
- } while (0);
- return true;
- }
ok 至此 在界面上一共添加了三个层,如果有啥问题 请指正 个人觉得 这些代码没啥技术含量,所以,不需要过多的去解释代码,主要的地方我都已经写了注释
接下来添加动作事件 让Bottom层动起来!Let's Go~
uilayer和bottomlayer都需要处理各自的触摸事件,为了正确的处理各自的触摸事件,需要使用TargetedTouchHandlers,通过设置优先级,可以确保uilayer在bottomlayer之前接收到触摸事件,
怎么设置优先级呢? 看代码
- //第二个参数越小,越先接收到事件
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, true);
当然 要让CCLayer能够接收触摸 需要添加setTouchEnabled(true);
第一步 我们先修改UIlayer的代码 让它可以最先接收到触控信息
- class UIlayer: public CCLayer {
- public:
- UIlayer();
- virtual ~UIlayer();
- bool init();
- //触摸事件
- virtual void registerWithTouchDispatcher();
- //用于判断是否触摸到node
- bool isTouchForMe(CCPoint point);
- virtual bool ccTouchBegan(CCTouch * pTouch, CCEvent *pEvent);
- virtual void ccTouchEnded(CCTouch * pTouch, CCEvent *pEvent);
- CREATE_FUNC(UIlayer)
- ;
- private:
- };
- bool UIlayer::init() {
- bool bRet = false;
- do {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSprite *sprite = CCSprite::create("layer2.jpg");
- sprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(sprite, 0, TargetSprite2);
- //让UIlayer可以接收到触摸事件
- setTouchEnabled(true);
- bRet = true;
- } while (0);
- return bRet;
- }
- void UIlayer::registerWithTouchDispatcher() {
- //设置优先级
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
- this, -1, true);
- }
- bool UIlayer::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent) {
- CCLog("UIlayer触摸事件");
- CCPoint point = pTouch->getLocationInView();
- //判断触摸点是否在精灵内
- bool isTouchHandled = UIlayer::isTouchForMe(point);
- if (isTouchHandled) {
- //根据添加精灵时设置的tag 从当前层中根据tag获取到精灵
- CCNode *node = this->getChildByTag(TargetSprite2);
- //设置高亮
- ((CCSprite *) node)->setColor(ccRED);
- }
- return isTouchHandled;
- }
- void UIlayer::ccTouchEnded(CCTouch* pTouch, CCEvent *pEvent) {
- CCNode *node = this->getChildByTag(TargetSprite2);
- ((CCSprite *) node)->setColor(ccWHITE);
- }
- bool UIlayer::isTouchForMe(CCPoint point) {
- CCNode *node = this->getChildByTag(TargetSprite2);
- bool b = CCRect::CCRectContainsPoint(node->boundingBox(), point);
- return b;
- }
代码毫无疑问 如果对触摸事件监听不了解的 可以去其他博客参考
给出其中一个链接:点我看Cocos2dx的触控监听
UIlayer层主要的操作是 当触摸点在精灵上时 使精灵高亮,手指离开时恢复 不对Bottomlayer层进行操作
接下来是Bottomlayer层 当触摸点不在UIlayer层的精灵上时,可以拖动Bottomlayer层,本层的代码就不写了。
最后将完整代码写出,供参考
- /*
- * MainScene.h
- *
- * Created on: 2012-9-13
- * Author: Panda
- */
- #ifndef MAINSCENE_H_
- #define MAINSCENE_H_
- #include "cocos2d.h"
- USING_NS_CC;
- typedef enum {
- TargetLayerMin = 0,
- TargetLayerMain,
- TargetLayerGame,
- TargetLayerUI,
- TargetLayerMax,
- } TargetLayer;
- typedef enum {
- TargetSpriteMin = 0, TargetSprite1, TargetSprite2, TargetSpriteMax,
- } TargetSprite;
- class UIlayer: public CCLayer {
- public:
- UIlayer();
- virtual ~UIlayer();
- bool init();
- //触摸事件
- virtual void registerWithTouchDispatcher();
- //用于判断是否触摸到node
- bool isTouchForMe(CCPoint point);
- virtual bool ccTouchBegan(CCTouch * pTouch, CCEvent *pEvent);
- virtual void ccTouchEnded(CCTouch * pTouch, CCEvent *pEvent);
- CREATE_FUNC(UIlayer)
- ;
- private:
- };
- class Bottomlayer: public CCLayer {
- public:
- Bottomlayer();
- virtual ~Bottomlayer();
- bool init();
- //触摸事件
- virtual void registerWithTouchDispatcher();
- virtual bool ccTouchBegan(CCTouch * pTouch, CCEvent *pEvent);
- virtual void ccTouchMoved(CCTouch * pTouch, CCEvent *pEvent);
- virtual void ccTouchEnded(CCTouch * pTouch, CCEvent *pEvent);CREATE_FUNC(Bottomlayer)
- ;
- };
- class MainScene: public CCLayer {
- public:
- MainScene();
- virtual ~MainScene();
- virtual bool init();
- static CCScene *scene();
- virtual void registerWithTouchDispatcher();
- virtual bool ccTouchBegan(CCTouch * pTouch, CCEvent *pEvent);
- virtual void ccTouchEnded(CCTouch * pTouch, CCEvent *pEvent);
- CREATE_FUNC(MainScene)
- ;
- };
- #endif /* MAINSCENE_H_ */
- /*
- * MainScene.cpp
- *
- * Created on: 2012-9-13
- * Author: Panda
- */
- #include "MainScene.h"
- MainScene::MainScene() {
- // TODO Auto-generated constructor stub
- }
- MainScene::~MainScene() {
- // TODO Auto-generated destructor stub
- }
- bool MainScene::init() {
- bool bRet = false;
- do {
- setTouchEnabled(true);
- bRet = true;
- } while (0);
- return bRet;
- }
- CCScene *MainScene::scene() {
- CCScene *pScene = NULL;
- do {
- pScene = CCScene::create();
- MainScene *main = MainScene::create();
- Bottomlayer *game = Bottomlayer::create();
- UIlayer *ui = UIlayer::create();
- //将main层添加到最下层
- pScene->addChild(main, 0, TargetLayerMain);
- //game层为中间层
- pScene->addChild(game, 1, TargetLayerGame);
- //ui为最上层
- pScene->addChild(ui, 2, TargetLayerUI);
- } while (0);
- return pScene;
- }
- void MainScene::registerWithTouchDispatcher() {
- //设置优先级
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
- this, 1, true);
- }
- bool MainScene::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent) {
- CCLog("MainScene触摸事件");
- return true;
- }
- void MainScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
- }
- Bottomlayer::Bottomlayer() {
- }
- Bottomlayer::~Bottomlayer() {
- }
- bool Bottomlayer::init() {
- bool bRet = false;
- do {
- //添加一个精灵到Bottom层,不多解释 没啥问题
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSprite *sprite = CCSprite::create("layer1.jpg");
- sprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(sprite, 0, TargetSprite1);
- setTouchEnabled(true);
- bRet = true;
- } while (0);
- return bRet;
- }
- void Bottomlayer::registerWithTouchDispatcher() {
- //设置优先级
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
- this, 0, true);
- }
- bool Bottomlayer::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent) {
- CCLog("Bottomlayer触摸事件");
- return true;
- }
- void Bottomlayer::ccTouchMoved(CCTouch* pTouch, CCEvent *pEvent) {
- CCPoint point = pTouch->getLocationInView();
- CCNode *node = getChildByTag(TargetSprite1);
- ((CCSprite *) node)->setPosition(point);
- }
- void Bottomlayer::ccTouchEnded(CCTouch* pTouch, CCEvent *pEvent) {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCNode *node = getChildByTag(TargetSprite1);
- ((CCSprite *) node)->setPosition(ccp(size.width/2,size.height/2));
- }
- UIlayer::UIlayer() {
- }
- UIlayer::~UIlayer() {
- }
- bool UIlayer::init() {
- bool bRet = false;
- do {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- CCSprite *sprite = CCSprite::create("layer2.jpg");
- sprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(sprite, 0, TargetSprite2);
- //让UIlayer可以接收到触摸事件
- setTouchEnabled(true);
- bRet = true;
- } while (0);
- return bRet;
- }
- void UIlayer::registerWithTouchDispatcher() {
- //设置优先级
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(
- this, -1, true);
- }
- bool UIlayer::ccTouchBegan(CCTouch* pTouch, CCEvent *pEvent) {
- CCLog("UIlayer触摸事件");
- CCPoint point = pTouch->getLocationInView();
- //判断触摸点是否在精灵内
- bool isTouchHandled = UIlayer::isTouchForMe(point);
- if (isTouchHandled) {
- //根据添加精灵时设置的tag 从当前层中根据tag获取到精灵
- CCNode *node = this->getChildByTag(TargetSprite2);
- //设置高亮
- ((CCSprite *) node)->setColor(ccRED);
- }
- return isTouchHandled;
- }
- void UIlayer::ccTouchEnded(CCTouch* pTouch, CCEvent *pEvent) {
- CCNode *node = this->getChildByTag(TargetSprite2);
- ((CCSprite *) node)->setColor(ccWHITE);
- }
- bool UIlayer::isTouchForMe(CCPoint point) {
- CCNode *node = this->getChildByTag(TargetSprite2);
- bool b = CCRect::CCRectContainsPoint(node->boundingBox(), point);
- return b;
- }
接下来一章将讲解游戏关卡,如有问题,请提出
本教程根据Cocos2d教程翻译过来
使用的cocos2d-x版本为2.02