【cocos2dx】如何实现场景的跳转

本文介绍了一个使用Cocos2d-x的游戏启动场景(SplashScene)的设计与实现过程。该场景显示游戏名称和工作室名称,并通过定时器在3秒后自动跳转到游戏主场景。文章详细展示了如何创建文本标签、设置位置与缩放比例,以及如何使用调度器安排自动场景跳转。

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


前面定义了一个SpalshScene.h,并声明了如下的函数,在SplashScene.cpp中对这些函数实现,

跳转用到了CCNode类的定时器有关的函数,详细参考书,这里采用的函数为:

void scheduleOnce(SEL_SCHEDULE selector,  float delay)// 启动一次定时器,参数为回调函数及延时启动时间

#include "SplashScene.h"


bool Splash::init(){
	if (!Layer::init()){
		return false;
	}
	// show game name 
	auto labelGame = Label::createWithBMFont("futura-48.fnt", "2048");
	labelGame->setPosition(Point(GAME_SCREEN_WIDTH/2,GAME_SCREEN_HEIGHT/2));
	this->addChild(labelGame);
	labelGame->setScale(1.5);


	//show game studio
	auto labelGameVec = Label::createWithBMFont("futura-48.fnt", "ml_studio");
	labelGameVec->setPosition(Point(GAME_SCREEN_WIDTH / 2, GAME_SCREEN_HEIGHT / 4));
	this->addChild(labelGameVec);
	labelGameVec->setScale(0.8);

	//计划任务。3秒钟之后自动跳转到游戏场景。


	this->scheduleOnce(schedule_selector(Splash::jumpToGame), 1);// wait one second
	// jumpToGame(float) 在下面定义了

	return  true;

}

Scene * Splash::createScene(){
	auto scene = Scene::create();
	auto layer = Splash::create();
	scene->addChild(layer);
	return scene;
}

void Splash::jumpToGame(float t)//回调函数在这里
{
	auto scene = Game::createScene(); // Game is another Layer in 'GameScene.h'
	Director::getInstance()->replaceScene(TransitionProgressOutIn::create(0.5, scene));

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值