cocos2d-x-3.0rc1 学习笔记4(场景切换2)

前面的的别人写的,研究了一下有点懂了,然后自己仿造HelloWorld 写了一个,模板的是在AppDelegate中调用 scene ,我就在菜单按钮的回调函数里调用

.h 中

class Newscene : public cocos2d::Layer
{
public:
	// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
	virtual bool init();  

	// there's no 'id' in cpp, so we recommend returning the class instance pointer
	static cocos2d::Scene* scene();

	// a selector callback
	void menuCloseCallback(Ref* sender);
	void menuCloseCallback1(Ref* sender);

	// implement the "static node()" method manually
	CREATE_FUNC(Newscene);
};


cpp 中

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

	// 'layer' is an autorelease object
	Newscene *layer = Newscene::create();

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

	return scene;
}
bool Newscene::init()
{
	if ( !Layer::init() )
	{
		return false;
	}

	auto visibleSize = Director::getInstance()->getVisibleSize();
	auto origin = Director::getInstance()->getVisibleOrigin();

	//添加一个标题层
	auto cocoa=LabelTTF::create("HelpTxt", "Arial", TITLE_FONT_SIZE*2);
	cocoa->setPosition(Point(visibleSize.width/2,visibleSize.height/2+cocoa->getContentSize().height));
	this->addChild(cocoa,1);

	//背景精灵
	LayerColor* clo=LayerColor::create(Color4B(0,255,255,100),visibleSize.width,visibleSize.height);
	this->addChild(clo);

	//添加2个菜单按钮 	
	auto closeIt=MenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		CC_CALLBACK_1(Newscene::menuCloseCallback,this));


	auto closeIt1=MenuItemLabel::create(LabelTTF::create("Back", "Arial", TITLE_FONT_SIZE));
	closeIt1->setCallback(CC_CALLBACK_1(Newscene::menuCloseCallback1,this));  

	auto menuu=Menu::create(closeIt,closeIt1,NULL);
	closeIt->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+20));
	menuu->setPosition(Point(visibleSize.width/2,100));
	this->addChild(menuu,1);
	return true;
}

//退出help
void Newscene::menuCloseCallback(Ref* sender)
{
#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
}
//返回原先场景 help
void Newscene::menuCloseCallback1(Ref* sender)
{
#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

	auto sc=HelloWorld::scene();
	Director::sharedDirector()->replaceScene(TransitionRotoZoom::create(1.0,sc));

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

//调用一个新的场景
void HelloWorld::menuCloseCallback2(Ref* sender)
{
#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
     
	 auto sc=Scene::create();
	 Newscene* la=Newscene::create();
	 sc->addChild(la);
	 Director::sharedDirector()->replaceScene(CCTransitionFadeTR::create(1.0,sc));

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值