的Cocos2D-X:我怎样才能画出一个矩形调整?(Cocos2d-x: How can I dra

2019-10-22 06:09发布

我正在用的Cocos2D-X 3.4的一个项目(美妙框架BTW :))。 我想知道我怎么可以得出一个简单的半透明的选择,你可以在Windows上看到同样的选择?

http://cdn.maximumpcguides.com/windows-7/wp-content/uploads/2010/11/use-translucent-select-rectangle-2.png

我试图用DrawNode类,但未能实现这一点:“(我希望有人能告诉我该怎么做的正确方法,请:-)

Answer 1:

这是很容易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();
}


文章来源: Cocos2d-x: How can I draw a resizing rectangle?