【cocos2dx 3.2】一个都不能死6 主场景层

2015年03月17日 16:01 0 点赞 0 评论 更新于 2025-11-21 17:30

功能分析

在主场景中,我们的主要任务是将任意数量的游戏层添加到场景中,并设置相应的点击事件监听和物理碰撞监听。具体来说,点击屏幕后调用 jump() 方法,当发生物理碰撞时重新开始 HelloWorldScene

代码实现

1. HelloWorldScene.h 文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "GameLayer.h"

class HelloWorld : public cocos2d::LayerColor {
public:
// 创建场景的静态方法
static cocos2d::Scene* createScene();

// 初始化方法
virtual bool init();

// 自动生成创建对象的函数
CREATE_FUNC(HelloWorld);

// 关闭菜单回调函数
void menuCloseCallback(cocos2d::Ref* pSender);

// 用向量存储所有游戏层,方便管理
cocos2d::Vector<GameLayer*> games;
};

#endif // __HELLOWORLD_SCENE_H__

2. HelloWorldScene.cpp 文件

#include "HelloWorldScene.h"

USING_NS_CC;

// 创建场景的实现
Scene* HelloWorld::createScene() {
// 创建物理场景
auto scene = Scene::createWithPhysics();
// 设置重力
scene->getPhysicsWorld()->setGravity(cocos2d::Point(0, -1000));
// 设置调试绘制掩码,显示所有物理信息
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

// 创建 HelloWorld 层
auto layer = HelloWorld::create();
// 将层添加到场景中
scene->addChild(layer);

return scene;
}

// 初始化方法的实现
bool HelloWorld::init() {
// 初始化 LayerColor,设置背景颜色
if (!LayerColor::initWithColor(Color4B(255, 140, 0, 255))) {
return false;
}

// 初始化随机数种子
srand(time(NULL));

// 获取可见区域的大小和原点
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();

// 添加游戏层,每层设置不同高度
auto game = GameLayer::create(30);
games.pushBack(game);
addChild(game);

auto game2 = GameLayer::create(200);
games.pushBack(game2);
addChild(game2);

// 设置点击监听事件
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch *t, Event *e) {
// 遍历所有游戏层
for (auto it = games.begin(); it != games.end(); it++) {
// 检查点击位置是否在游戏层的边界框内
if ((*it)->getEdge()->getBoundingBox().containsPoint(t->getLocation())) {
// 调用 jump 方法
(*it)->jump();
}
}
return true;
};
// 添加点击事件监听器
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

// 设置物理碰撞监听事件
auto contactListener = EventListenerPhysicsContact::create();
contactListener->onContactBegin = [this](PhysicsContact &contact) {
// 遍历所有游戏层,停止更新
for (auto i = games.begin(); i != games.end(); i++) {
(*i)->unscheduleUpdate();
}
// 替换当前场景为新的 HelloWorld 场景
Director::getInstance()->replaceScene(HelloWorld::createScene());
return true;
};
// 添加物理碰撞事件监听器
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

return true;
}

// 关闭菜单回调函数的实现
void HelloWorld::menuCloseCallback(Ref* pSender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.", "Alert");
return;
#endif

// 结束游戏
Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

通过以上代码,我们实现了主场景层的基本功能,包括添加游戏层、处理点击事件和物理碰撞事件。这样,在游戏中玩家点击屏幕时,相应的游戏层会执行 jump() 方法,当发生物理碰撞时,游戏会重新开始。

作者信息

menghao

menghao

共发布了 3994 篇文章