- 各个层的创建和挂载
- 设置层初始化
- 初始化地图
- 键盘事件监听、控制玩家操作按钮的触摸监听(触摸按下、触摸持续、触摸抬起)
- update实时检测游戏是否被暂停(以便于接触暂停后恢复数据)
- 根据按键\按钮左右方向的控制
- 各种按钮的回调
USING_NS_CC;
class Hero;
class HudLayer;
class GameScene : public cocos2d::LayerColor
{
private:
bool pause; //暂停标识
cocos2d::Layer* staticLayer; //静态层 - 基层
cocos2d::Layer* gameLayer; //游戏主层--地图层
cocos2d::Layer* bkLayer; //背景层-视差层
cocos2d::Layer* settingLayer; //设置层
cocos2d::Layer* weaponLayer; //特效层
HudLayer* hudLayer; //hud层
cocos2d::Layer* heroLayer; //英雄层
cocos2d::Layer* monsterLayer; //怪物层
cocos2d::Layer* thingLayer; //事物层
Hero* hero; //英雄
bool _left;
bool _right;
public:
static int level;
//根据关卡创建游戏场景:创建出各个层,传给游戏管理者GameManager,然后挂载到其父节点上
static Scene* createScene(int level);
virtual bool init();
CREATE_FUNC(GameScene);
void update(float time);
//左右方向
void setLeft(bool left);
bool isLeft();
void setRight(bool right);
bool isRight();
//暂停层的按钮回调
void pauseButton(Ref* ref);
void backButton(Ref* ref);
void homeButton(Ref* ref);
void breakButton(Ref* ref);
};
USING_NS_CC;
//在关卡点击的回调函数中调用
Scene* GameScene::createScene(int level)
{
GameScene::level = level;
auto scene = Scene::create();
auto layer = GameScene::create();
scene->addChild(layer);
return scene;
}
bool GameScene::init()
{
if (!LayerColor::initWithColor(Color4B(65, 105, 225, 255)))
{
return false;
}
pause = false; //暂停标识为假
staticLayer = this; //游戏静态层
GameManager::getInstance()->staticLayer = staticLayer;
gameLayer = Layer::create(); //游戏主层(挂载到游戏静态层)
GameManager::getInstance()->gameLayer = gameLayer;
staticLayer->addChild(gameLayer);
hudLayer = HudLayer::create(); //HUD层(挂载到游戏静态层)
GameManager::getInstance()->hudLayer = hudLayer;
staticLayer->addChild(hudLayer);
settingLayer = Layer::create(); //设置层(挂载到游戏静态层)
GameManager::getInstance()->settingLayer = settingLayer;
staticLayer->addChild(settingLayer);
//视差节点
ParallaxNode* praNode = ParallaxNode::create();
bkLayer = Layer::create(); //背景层
GameManager::getInstance()->bkLayer = bkLayer;
gameLayer->addChild(