这一节我们来具体说说Scene,简单来说Scene就是一个容器,其中可以容纳Sprite、Label、Node等各种游戏中需要的对象。Scene还负责控制游戏的逻辑和内部对象的渲染,游戏中至少有一个Scene,也可以有多个Scene互相切换,并且在Scene切换的过程中还可以指定炫酷的效果。下面的代码创建了一个Scene
auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); scene1 = Scene::create(); auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto thisSceneLabel = Label::createWithTTF("This Is A Scene","Marker Felt.ttf",32); thisSceneLabel->setPosition(Vec2(visibleSize.width / 2,visibleSize.height / 2)); scene1->addChild(thisSceneLabel,1); auto menuItem1 = MenuItemFont::create("Make A New Scene"); menuItem1->setFontNameObj("Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height / 2).x,(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height).y - (index) * 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { Director::getInstance()->replaceScene(createScene2()); }); auto menu = Menu::create(menuItem1,NULL); menu->setName("menu"); menuNode->addChild(menu,1); menu->setPosition(Vec2::ZERO); scene1->addChild(menuNode,2);创建结果如下
第24、25、26行设置了菜单的回调函数,当点击菜单时就会执行替换Scene的操作。replaceScene函数用于直接替换Scene,不添加任何效果。替换效果如下
创建这个Scene的代码如下
auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); scene2 = Scene::create(); scene2->retain(); auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto thisSceneLabel = Label::createWithTTF("This Is A New Scene",visibleSize.height / 2)); scene2->addChild(thisSceneLabel,1); auto menuItem1 = MenuItemFont::create("Make a New Scene Using a Transition Fade"); menuItem1->setFontNameObj("Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height).y - (index)* 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { Director::getInstance()->replaceScene(TransitionFade::create(0.5,createScene3(),Color3B(0,255,255))); }); auto menu = Menu::create(menuItem1,1); menu->setPosition(Vec2::ZERO); scene2->addChild(menuNode,2); return scene2;注意27行菜单回调函数的内容,这里使用了TransitionFade::create来创建了一个0.5秒长的渐变褪色效果,点击菜单效果如下
创建这个Scene的代码如下
auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); scene3 = Scene::create(); scene3->retain(); auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto thisSceneLabel = Label::createWithTTF("This Scene used a Transition!",visibleSize.height / 2)); scene3->addChild(thisSceneLabel,1); auto menuItem1 = MenuItemFont::create("Make a New Scene Using a Flip"); menuItem1->setFontNameObj("Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height).y - (index)* 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { Director::getInstance()->replaceScene(TransitionFlipX::create(2,createScene4())); }); auto menu = Menu::create(menuItem1,1); menu->setPosition(Vec2::ZERO); scene3->addChild(menuNode,2); return scene3;在27行的菜单回调函数中使用TransitionFlipX::create函数创建了一个2秒的翻转效果,点击菜单执行效果如下
除了上面介绍的3种切换Scene的方法,还有如下方法来用于切换Scene。
TransitionSlideInT::create用来创上下欢动切换效果。
pushScene用于把已经存在的Scene放到场景堆栈中,之后可以用popScene把场景堆栈中的Scene取出。
下一节我们将讲述其它和UI有关的部分。
原文链接:https://www.f2er.com/cocos2dx/341910.html