本文主要是介绍2014-12-8课堂笔记:转换场景的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
步骤:
一:拷贝--粘贴----修改文件名
二:修改类名
三:在HelloWorld.h中包含MyScene.h文件;
四:修改HelloWorld.cpp文件中的菜单回调函数
1:全部删除;
2:加入:
auto m=MyScene::createScene();
Director::getInstance()->replaceScene(m)
HelloWorld.cpp文件:
#include "HelloWorldScene.h"
#include "MyScene.h"
//转换场景的实现。
USING_NS_CC;Scene* HelloWorld::createScene()
{// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = HelloWorld::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;
}// on "init" you need to initialize your instance
bool HelloWorld::init()
{//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();/// 2. add a menu item with "X" image, which is clicked to quit the program// you may modify it.// add a "close" icon to exit the progress. it's an autorelease objectauto closeItem = MenuItemImage::create("CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,origin.y + closeItem->getContentSize().height/2));// create menu, it's an autorelease objectauto menu = Menu::create(closeItem, NULL);menu->setPosition(Vec2::ZERO);this->addChild(menu, 1);/// 3. add your codes below...// add a label shows "Hello World"// create and initialize a labelauto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);// position the label on the center of the screenlabel->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));// add the label as a child to this layerthis->addChild(label, 1);// add "HelloWorld" splash screen"auto sprite = Sprite::create("HelloWorld.png");// position the sprite on the center of the screensprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));// add the sprite as a child to this layerthis->addChild(sprite, 0);return true;
}void HelloWorld::menuCloseCallback(Ref* pSender)
{auto m=MyScene::createScene();Director::getInstance()->replaceScene(m); //函数形式
}
HelloWorld.h文件:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"class HelloWorld : public cocos2d::Layer
{
public:// there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::Scene* createScene();// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init();// a selector callbackvoid menuCloseCallback(cocos2d::Ref* pSender);// implement the "static create()" method manuallyCREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__
MyScene.cpp文件:
#include "MyScene.h"USING_NS_CC;Scene* MyScene::createScene()
{// 'scene' is an autorelease objectauto scene = Scene::create();// 'layer' is an autorelease objectauto layer = MyScene::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;
}// on "init" you need to initialize your instance
bool MyScene::init()
{//// 1. super init firstif ( !Layer::init() ){return false;}Size visibleSize = Director::getInstance()->getVisibleSize();Vec2 origin = Director::getInstance()->getVisibleOrigin();/// 2. add a menu item with "X" image, which is clicked to quit the program// you may modify it.// add a "close" icon to exit the progress. it's an autorelease objectauto closeItem = MenuItemImage::create("CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(MyScene::menuCloseCallback, this));closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,origin.y + closeItem->getContentSize().height/2));// create menu, it's an autorelease objectauto menu = Menu::create(closeItem, NULL);menu->setPosition(Vec2::ZERO);this->addChild(menu, 1);/// 3. add your codes below...// add a label shows "Hello World"// create and initialize a labelauto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);// position the label on the center of the screenlabel->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));// add the label as a child to this layerthis->addChild(label, 1);// add "MyScene" splash screen"auto sprite = Sprite::create("CloseNormal.png");// position the sprite on the center of the screensprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));// add the sprite as a child to this layerthis->addChild(sprite, 0);return true;
}void MyScene::menuCloseCallback(Ref* pSender)
{
#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;
#endifDirector::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);
#endif
}
MyScene.h文件
#ifndef __MyScene_SCENE_H__
#define __MyScene_SCENE_H__#include "cocos2d.h"class MyScene : public cocos2d::Layer
{
public:// there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::Scene* createScene();// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init();// a selector callbackvoid menuCloseCallback(cocos2d::Ref* pSender);// implement the "static create()" method manuallyCREATE_FUNC(MyScene);
};#endif // __MyScene_SCENE_H__
这篇关于2014-12-8课堂笔记:转换场景的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!