layer之间切换

看了 偶尔e网事 童鞋的那篇博客,我也依葫芦画瓢。

就是点击不同的按钮切换场景:

场景的代码:

#include "TestLayer.h"


bool TestLayer::init(const std::string& layerName,const std::string& imageFolder)
{
	bool bRet=false;
	do 
	{
		if(!CCLayer::init())
		{
			return false;
		}

		CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
		CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

		CCLabelTTF* pLabel = CCLabelTTF::create(layerName.c_str(), "Arial", 30);

		// position the label on the center of the screen
		pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
			origin.y + visibleSize.height - pLabel->getContentSize().height));

		// add the label as a child to this layer
		this->addChild(pLabel, 1);

		// add "HelloWorld" splash screen"
		CCSprite* pSprite = CCSprite::create(imageFolder.c_str());
		pSprite->setScale(0.3);

		// position the sprite on the center of the screen
		pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

		// add the sprite as a child to this layer
		this->addChild(pSprite, 0);
		bRet=true;
	} while (0);
	return bRet;
}

TestLayer* TestLayer::create( const std::string& layerName,const std::string& imageFolder )
{
	TestLayer* pRet = new TestLayer;
	if (pRet && pRet->init(layerName, imageFolder))
	{
		pRet->autorelease();
	}
	else
	{
		delete pRet;
		pRet = nullptr;
	}
	return pRet;
}

加载的代码:

#include "HelloWorldScene.h"
#include "TestLayer.h"

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

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

	curLayerPos = 0;
    
	TestLayer* layer1 = TestLayer::create("1","1.jpg");
	TestLayer* layer2 = TestLayer::create("2","2.jpg");
	TestLayer* layer3 = TestLayer::create("3","3.jpg");

	_layerMuticomplex = LayerMultiplex::create();
	_layerMuticomplex->addLayer(layer1);
	_layerMuticomplex->addLayer(layer2);
	_layerMuticomplex->addLayer(layer3);

	_layerMuticomplex->switchTo(0);

	addChild(_layerMuticomplex,1);

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

	Sprite* pSprite = Sprite::create("4.jpg");
	addChild(pSprite,0);

	MenuItemImage *pCloseItem1 = MenuItemImage::create(
		"CloseNormal.png",
		"CloseNormal.png",
		this,
		menu_selector(HelloWorld::menuCallback));

	MenuItemImage *pCloseItem2 = MenuItemImage::create(
		"CloseNormal.png",
		"CloseNormal.png",
		this,
		menu_selector(HelloWorld::menuCallback));

	MenuItemImage *pCloseItem3 = MenuItemImage::create(
		"CloseNormal.png",
		"CloseNormal.png",
		this,
		menu_selector(HelloWorld::menuCallback));

	// create menu, it's an autorelease object
	pCloseItem1->setTag(0);
	pCloseItem2->setTag(1);
	pCloseItem3->setTag(2);
	Menu* pMenu = CCMenu::create(pCloseItem1, pCloseItem2, pCloseItem3, NULL);
	pMenu->alignItemsHorizontallyWithPadding(30);
	pMenu->setPosition(visibleSize.width/2,25);
	this->addChild(pMenu,1);
    
    return true;
}


void HelloWorld::menuCloseCallback(Object* pSender)
{
    Director::getInstance()->end();

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

void HelloWorld::menuCallback( CCObject* pSender )
{
	const int item=((CCMenuItemImage*)pSender)->getTag();
	if (item!=curLayerPos)
	{
		_layerMuticomplex->switchTo(item);
		curLayerPos=item;
	}
}

效果图就不展示了,很简单。

 

你可能感兴趣的:(layer之间切换)