cocos2d-x scene场景的切换

本文详细介绍了如何使用cocos2d-x框架进行游戏场景的切换,从创建新工程到修改HelloWorldScene.cpp文件,利用lambda表达式处理点击事件,并通过Director进行场景替换。提供了在Android和Ubuntu上的运行示例,附带源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇中我们添加两个新类,但是类并没有做任何事情事情,这篇文章主要介绍如何切换游戏的画面,比如从开始切换到游戏界面。

还是命令行新建工程

cocos new SimpleDemo  -p com.MyCompany.SimpleDemo -l cpp -d ./MyCompany

cocos compile -s ./MyCompany/SimpleDemo -p linux -m debug -o ./MyCompany/SimpleDemo/bin

建立完成之后生成四个class文件,前面的文章有介绍,这里不再介绍。

AppDelegate.cpp基本上不需要修改,可能根据需要修改屏幕尺寸。

打开HelloWorldScene.cpp文件

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "Chapter2Level1.h"
#include "Chapter3.h"
#include "Chapter2.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
                                CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    // add main menu
	auto mainMenu = Menu::create();
	int index = 2;

	std::vector<std::string> vChapters = {"Chapter 2", "Chapter 3"};
	std::vector<MenuItemLabel *> vMenuItems;
	// create the menu items for each other
	for (unsigned int i = 0; i < vChapters.size(); i++) {
    	auto itemLabel = LabelTTF::create(vChapters.at(i), "Arial", 32);
		auto menuIte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值