[英]Cocos2d-x: How can I draw a resizing rectangle?
我正在使用Cocos2d-x 3.4(精彩框架BTW :)开展项目。 我想知道如何绘制一个简单的半透明选择,你可以在Windows上看到相同的选择?
我试图使用DrawNode类,但未能实现这一点:'(我希望有人能告诉我正确的方法,请:-)
使用DrawNode绘制非常简单。
在onTouchBegan事件上设置原点,并在onTouchMoved事件上设置目标点。
// HelloWorld.h
class HelloWorld : public Layer{
public:
...
bool onTouchBegan(const Touch *touch, Event *event);
void onTouchMoved(const Touch *touch, Event *event);
void onTouchEnded(const Touch *touch, Event *event);
protected:
Vec2 _originPoint;
Vec2 _destinationPoint;
DrawNode *_drawNode;
};
// HelloWorld.cpp
bool HelloWorld::init()
{
if ( !Layer::init() ) return false;
// Add touch listener
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// Create the draw node
_drawNode = DrawNode::create();
addChild(_drawNode);
return true;
}
bool HelloWorld::onTouchBegan(const cocos2d::Touch *touch, cocos2d::Event *event)
{
_originPoint = touch->getLocation();
_destinationPoint = _originPoint;
return true;
}
void HelloWorld::onTouchMoved(const cocos2d::Touch *touch, cocos2d::Event *event)
{
_destinationPoint = touch->getLocation();
_drawNode->clear();
_drawNode->drawSolidRect(_originPoint, _destinationPoint, Color4F(0,0,1,0.2));
_drawNode->drawRect(_originPoint, _destinationPoint, Color4F::BLUE);
}
void HelloWorld::onTouchEnded(const cocos2d::Touch *touch, cocos2d::Event *event)
{
_drawNode->clear();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.