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

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);
}

简单效果图展示 如下:



转载于:https://www.cnblogs.com/Anzhongliu/p/6091799.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值