关于在c++11中调用replaceScene跳转场景两次初始化init()问题

continueGame->addTouchEventListener([&](Ref* r, Widget::TouchEventType type){
		auto scene = GameScene::createScene();
		switch (type)
		{
		case cocos2d::ui::Widget::TouchEventType::BEGAN:
			Director::getInstance()->replaceScene(scene);/**/
			break;
		case cocos2d::ui::Widget::TouchEventType::MOVED:
			break;
		case cocos2d::ui::Widget::TouchEventType::ENDED:
			break;
		case cocos2d::ui::Widget::TouchEventType::CANCELED:
			break;
		default:
			break;
		}
	});

由于新场景的创建是在一个按钮的匿名函数里面,而switch里面又不能创建变量,所以我把变量的创建写在了匿名函数内部,如上,但其实点击该按钮的时候,会调用两次该按钮,具体原因我也不是很清楚,有知道的朋友希望留个言告知下!

解决办法:如下:

continueGame->addTouchEventListener([&](Ref* r, Widget::TouchEventType type){
		//auto scene = GameScene::createScene();
		switch (type)
		{
		case cocos2d::ui::Widget::TouchEventType::BEGAN:
			Director::getInstance()->replaceScene( GameScene::createScene());/**/
			break;
		case cocos2d::ui::Widget::TouchEventType::MOVED:
			break;
		case cocos2d::ui::Widget::TouchEventType::ENDED:
			break;
		case cocos2d::ui::Widget::TouchEventType::CANCELED:
			break;
		default:
			break;
		}
	});
或者把新场景的创建写到匿名函数外面

你可能感兴趣的:(studio)