同一场景下多个图层之间的调用

test01.h文件

#pragma once
#ifndef test01_h__
#define test01_h__
#include "cocos2d.h"
USING_NS_CC;
class test01 : public cocos2d::CCLayer
{
public:
	virtual bool init();
	CREATE_FUNC(test01);
	void menuCloseCallback(cocos2d::Ref* pSender);

};

#endif // FishLayer_h__


test01.cpp文件

#include "test01.h"
bool test01::init()
{	
	Size visibleSize = Director::getInstance()->getVisibleSize();
	//获取可见区域原点坐标
	Point origin = Director::getInstance()->getVisibleOrigin();
	CCSprite* bg = CCSprite::create("airfightSheet.png");
	bg->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	addChild(bg);
	////////////
	auto closeItem = MenuItemImage::create(
		"start-up.png",
		"start-down.png",
		CC_CALLBACK_1(test01::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 object
	auto menu = Menu::create(closeItem, NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);


	return true;
}

void test01::menuCloseCallback(Ref* pSender)
{
	Director::getInstance()->end();
}

test02.h文件

#ifndef test02_h__
#define test02_h__
#include "cocos2d.h"
USING_NS_CC;
class test02 : public cocos2d::CCLayer
{
public:
	virtual bool init();
	CREATE_FUNC(test02);
};

#endif // BackgroundLayer_h__

test02.cpp文件

#include "test02.h"
bool test02::init()
{	
	Size visibleSize = Director::getInstance()->getVisibleSize();
	//获取可见区域原点坐标
	Point origin = Director::getInstance()->getVisibleOrigin();
	CCSprite* bg = CCSprite::create("123.png");
	bg->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
	addChild(bg);
	return true;
}


场景------HelloWorldScene.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "test01.h"
#include "test02.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCScene
{
public:
	test01* player01;
	virtual bool init();
	static CCScene* playGame();//创建场景
	void menuCloseCallback(cocos2d::Ref* pSender);
};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp文件

#include "HelloWorldScene.h"
#include "DrawNode3D.h"

CCScene* HelloWorld::playGame()
{
	HelloWorld* scene = new HelloWorld();
	scene->init();
	return scene;
}
bool HelloWorld::init()
{	
	Size visibleSize = Director::getInstance()->getVisibleSize();
	//获取可见区域原点坐标
	Point origin = Director::getInstance()->getVisibleOrigin();

	test02* player02 = test02::create();
	player01 = test01::create();

	
	addChild(player02);
	addChild(player01);
	/////////////////////
	auto closeItem = MenuItemImage::create(
		"start-up.png",
		"start-down.png",
		CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	closeItem->setPosition(Vec2( visibleSize.width / 2,
		visibleSize.height / 2));

	// create menu, it's an autorelease object
	auto menu = Menu::create(closeItem, NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);

	return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
	player01->setVisible(false);
}

简单效果图展示 如下:



posted @ 2016-05-12 22:48  Anzhongliu  阅读(221)  评论(0编辑  收藏  举报