cocos 3.2游戏暂停功能

前端之家收集整理的这篇文章主要介绍了cocos 3.2游戏暂停功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1,GamePauseUtils.h

//
//  GamePauseUtils.h
//  AdventureKing
//
//  Created by jianan on 15/5/12.
//
//

#ifndef __AdventureKing__GamePauseUtils__
#define __AdventureKing__GamePauseUtils__

#include "cocos2d.h"
USING_NS_CC;

//操作类型
typedef enum OPERATE_TYPE{
    OPERATE_TYPE_PAUSE,OPERATE_TYPE_RESUME
}OPERATE_TYPE;


//
class GamePauseUtils
{
public:
    GamePauseUtils();
    ~GamePauseUtils();
    static GamePauseUtils* getInstance();
    void operateAllSchedulerAndActions(Node* node,OPERATE_TYPE type,int ignoreTag); //忽略检测的tag
};


#endif /* defined(__AdventureKing__GamePauseUtils__) */

2,GamePauseUtils.cpp

//
//  GamePauseUtils.cpp
//  AdventureKing
//
//  Created by jianan on 15/5/12.
//
//

#include "GamePauseUtils.h"

static GamePauseUtils* instance = NULL;

GamePauseUtils::GamePauseUtils(){
    
}

GamePauseUtils::~GamePauseUtils(){
    
}

GamePauseUtils* GamePauseUtils::getInstance(){
    if(instance == NULL){
        instance = new GamePauseUtils();
    }
    return instance;
}

void GamePauseUtils::operateAllSchedulerAndActions(Node* node,int ignoreTag){
    if(node->isRunning()){
        switch(type){
            case OPERATE_TYPE_PAUSE:
                node->pause();
                break;
            case OPERATE_TYPE_RESUME:
                node->resume();
                break;
        }
        
        
        Vector<Node*> array = node->getChildren();
        
        if(array.size() > 0){
            for(Vector<Node*>::iterator iter = array.begin(); iter != array.end();){
                Node* child = *iter;
                
                if(child->getTag() == ignoreTag){
                    iter++;
                }else{
                    iter++;
                    this->operateAllSchedulerAndActions(child,type,ignoreTag);
                }
            }
        }
    }
}
3,暂停
GamePauseUtils::getInstance()->operateAllSchedulerAndActions(rootLayer,OPERATE_TYPE_PAUSE,IGNORE_TAG);
4,恢复
 GamePauseUtils::getInstance()->operateAllSchedulerAndActions(rootLayer,OPERATE_TYPE_RESUME,IGNORE_TAG);

猜你在找的Cocos2d-x相关文章