How to render black screen with alpha in cocos2dx
To render black screen with alpha
use ui::Layout Like View.
setBackGroundColor and setOpacity
Let's see the sample code.
ui::Layout* blackView = Layout::create();
blackView->setContentSize(Size(DISPLAY_WIDTH,DISPLAY_HEIGHT));
blackView->setBackGroundColor(Color3B::BLACK);
// need backGroundColorType
blackView->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
blackView->setOpacity(150);
blackView->setVisible(false);
this->addChild(blackView);
Point is setBackGroundColorType
code is
void Layout::setBackGroundColorType(BackGroundColorType type)
{
if (_colorType == type)
{
return;
}
switch (_colorType)
{
case BackGroundColorType::NONE:
if (_colorRender)
{
removeProtectedChild(_colorRender);
_colorRender = nullptr;
}
if (_gradientRender)
{
removeProtectedChild(_gradientRender);
_gradientRender = nullptr;
}
break;
case BackGroundColorType::SOLID:
if (_colorRender)
{
removeProtectedChild(_colorRender);
_colorRender = nullptr;
}
break;
case BackGroundColorType::GRADIENT:
if (_gradientRender)
{
removeProtectedChild(_gradientRender);
_gradientRender = nullptr;
}
break;
default:
break;
}
_colorType = type;
switch (_colorType)
{
case BackGroundColorType::NONE:
break;
case BackGroundColorType::SOLID:
_colorRender = LayerColor::create();
_colorRender->setContentSize(_contentSize);
_colorRender->setOpacity(_cOpacity);
_colorRender->setColor(_cColor);
addProtectedChild(_colorRender, BCAKGROUNDCOLORRENDERER_Z, -1);
break;
case BackGroundColorType::GRADIENT:
_gradientRender = LayerGradient::create();
_gradientRender->setContentSize(_contentSize);
_gradientRender->setOpacity(_cOpacity);
_gradientRender->setStartColor(_gStartColor);
_gradientRender->setEndColor(_gEndColor);
_gradientRender->setVector(_alongVector);
addProtectedChild(_gradientRender, BCAKGROUNDCOLORRENDERER_Z, -1);
break;
default:
break;
}
}
if set BackGroundColorType::SOLID
create LayoutColor object and render color.
This is available in cocos2dx 3.15.1