cocos2dx 新手引导层

#ifndef __Test_ClipNode_H__
#define __Test_ClipNode_H__

#include "cocos2d.h"
USING_NS_CC;
class GuideLayer : public cocos2d::Layer
{
public:
	GuideLayer();
	~GuideLayer();

	/*
	*color 层颜色
	*rect  需要显示的区域
	*/
	static Layer* create(Color4B &color, Rect &rect);
	bool init(Color4B &color, Rect &rect);
	void setRectangle(Rect &rect);
	

protected:
	Rect _unShowRect;
};
#endif
</pre><pre name="code" class="cpp"><strong>cpp文件</strong>
<pre name="code" class="cpp">#include "GuideLayer.h"

USING_NS_CC;
#define kTagClipperNode 1000

GuideLayer::GuideLayer()
{

}


GuideLayer::~GuideLayer()
{

}

Layer* GuideLayer::create(Color4B &color, Rect &rect)
{
    // 'scene' is an autorelease object
	auto layer = new GuideLayer();
    
	if (layer && layer->init(color,rect))
	{
		layer->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(layer);
	}
	
	return layer;
}

void GuideLayer::setRectangle(Rect &rect)
{
	_unShowRect = rect;
}


bool GuideLayer::init(Color4B &color, Rect &rect)
{

	if (!Layer::init())
	{
		return false;
	}
	auto visibleSize = Director::getInstance()->getVisibleSize();
	setRectangle(rect);

	auto clipper = ClippingNode::create();
	
	clipper->setContentSize(visibleSize);
	clipper->setAnchorPoint(Vec2(0.5, 0.5));
	clipper->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
	clipper->setInverted(true);
	this->addChild(clipper);

	auto stencil = DrawNode::create();
	Vec2 rectangle[4];
	rectangle[0] = Vec2(_unShowRect.origin.x, _unShowRect.origin.y);
	rectangle[1] = Vec2(_unShowRect.size.width + _unShowRect.origin.x, _unShowRect.origin.y);
	rectangle[2] = Vec2(_unShowRect.size.width + _unShowRect.origin.x, _unShowRect.size.height + _unShowRect.origin.y);
	rectangle[3] = Vec2(_unShowRect.origin.x, _unShowRect.size.height + _unShowRect.origin.y);

	Color4F white(1, 1, 1, 1);
	stencil->drawPolygon(rectangle, 4, white, 1, white);
	clipper->setStencil(stencil);


	auto backGroundLayer = LayerColor::create(color, visibleSize.width, visibleSize.height);//被裁剪的内容
	clipper->addChild(backGroundLayer);//5
	
	return true;
}

使用示例:
 
 
//添加新手引导层
	auto rectLayer = GuideLayer::create(Color4B(123, 123, 123, 255), Rect(429, 608, 110, 36));
	rectLayer->setPosition(Point::ZERO);
	this->addChild(rectLayer, 2);


	//添加提示语
	auto labelTip = Label::createWithTTF("Tips : this is Hello World", "fonts/Marker Felt.ttf", 24);
	labelTip->setColor(ccc3(255,0,0));
	labelTip->setPosition(Vec2(origin.x + visibleSize.width / 2,
		origin.y + visibleSize.height - label->getContentSize().height * 2));
	rectLayer->addChild(labelTip, 1);


 
 
运行效果
 
 
 
 
 
 

cocos2dx 新手引导层_第1张图片

你可能感兴趣的:(cocos2dx 新手引导层)