Layers with colors in the game

The visual part in Cocos2dx is mainly composed of scenes, layers and sprites. A layer is a container with two main things: (1) respond to user touch (2) layer with color.

Here's a look at layers with colors (LayerColor):

LayerColor: Creates a layer with color and opacity. (Purpose: Create pause interface and modal workbox)

//Create a colored layer
auto color = LayerColor::create();
color->initWithColor(Color4B(162, 162, 162, 128)); //translucent gray;

-----------------------------------------------------------------------------------------------------------------------------------------------

Let's implement an example: Clicking on the menu in the game scene will make the game screen gray (a layer with color is created), and then click on the layer with color to return to the game scene. (It doesn't matter if you don't understand touch events, will be

said)

Ideas: a. Create a background and menu 1 in the game scene b, create a layer with color in the callback function of menu 1, add menu 2 to the layer with color, and make a color-coded layer in the callback function of menu 2 layer moving, add this in the final game scene

layers with color

void Test::onEnter()
{
	Layer::onEnter();
	Size visibleSize = Director::getInstance()->getVisibleSize();
	//load background
	auto back = Sprite::create("background.png");
	back->setAnchorPoint(Vec2(0,0));
	this->addChild(back);

	//pause button
	auto menuItem = MenuItemImage::create("start-up.png","start-down.png",CC_CALLBACK_1(Test::selecedMenu,this));
	menuItem->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
	menuItem->setScale(0.5);
	auto menu = Menu::create(menuItem,NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu);

}
void Test::selecedMenu(cocos2d::Ref * ref)
{
	//Create a colored layer
	color = LayerColor::create();
	color->initWithColor(Color4B(162, 162, 162, 128)); //translucent gray;
	auto menuItem = MenuItemImage::create("dir.png", "dir.png", CC_CALLBACK_1(Test::selecedMenu1, this));
	menuItem->setPosition(Vec2(100, 500));
	auto menu = Menu::create(menuItem, NULL);
	menu->setPosition(Vec2::ZERO);
	color->addChild(menu);
	this->addChild(color, 5);
}
void Test::selecedMenu1(cocos2d::Ref * ref)
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	auto moveTo = MoveBy::create(0.5, Vec2(0, visibleSize.height));
	color->runAction(moveTo);
}

(The application of this layer with color in the mini-game will be added later)
  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325607443&siteId=291194637