微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【cocos2d-x 3.7 飞机大战】 决战南海I (十一) 关于游戏场景

这个场景作为弹出场景,主要介绍下游戏。。。还有自己的联系方式(*^__^*) ……

主要使用文本,就一个返回按键的响应函数


//返回按钮
void back(EventKeyboard::KeyCode keyCode,Event* pEvent);


同样要记得在析构函数中移除监听
AboutGame::~AboutGame()
{
	_eventdispatcher->removeEventListenersForTarget(this);
}



文本已在XML文件生成,直接调用即可
bool AboutGame::init()
{
	if (!Layer::init())
		return false;

	//背景音乐
	CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
	CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/BackgroundMusic1.mp3",true);
	
	bool bRect = false;

	do 
	{
		auto size = Director::getInstance()->getWinSize();

		//设置背景图片
		auto m_background = Sprite::createWithSpriteFrameName("backgroundTollgateOne.png");
		m_background->setPosition(Vec2(size.width/2,size.height/2));
		m_background->setAnchorPoint(Vec2(0.5,0.5));
		this->addChild(m_background);

		//设置监听器
		auto m_listener = EventListenerKeyboard::create();
		m_listener->onkeyreleased = CC_CALLBACK_2(AboutGame::back,this);
		_eventdispatcher->addEventListenerWithSceneGraPHPriority(m_listener,this);

		//设置标签获取中文文本
		auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");

		auto m_label3 = Label::createWithTTF(
			((__String*)(dictionary->objectForKey("Others")))->getCString(),"fonts/DFPShaoNvW5-GB.ttf",25
			);
		m_label3->setDimensions(size.width / 3 * 2,size.height / 2);
		m_label3->setColor(Color3B(255,255,255));
		m_label3->setPosition(Point(size.width / 2,size.height/2));

		this->addChild(m_label3);

		auto m_label1 = Label::createWithTTF(
			((__String*)(dictionary->objectForKey("AboutMe")))->getCString(),35
			);
		m_label1->setColor(Color3B(255,0));
		m_label1->setPosition(
			Point(size.width/2,size.height-m_label3->getContentSize().height-m_label1->getContentSize().height)
			);
		
		this->addChild(m_label1);

		auto m_label2 = Label::createWithTTF(
			((__String*)(dictionary->objectForKey("QQ")))->getCString(),25
			);
		m_label2->setDimensions(size.width / 3 * 2,size.height / 6);
		m_label2->setColor(Color3B(0,0));
		m_label2->setPosition(Point(size.width / 2,m_label1->getPositionY() - m_label2->getContentSize().height));

		this->addChild(m_label2);

		bRect = true;
	} while (0);

	return bRect;
}

//返回按钮
void AboutGame::back(EventKeyboard::KeyCode keyCode,Event* pEvent)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE)
	{
		//背景音乐
		CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
		CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_start.mp3",true);
		Director::getInstance()->popScene();
	}
		
}

原文地址:https://www.jb51.cc/cocos2dx/341638.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐