cocos2d-x-3.0 draw vs onDraw

后端 未结 4 777
心在旅途
心在旅途 2020-12-29 00:02

I\'m using cocos2d-x v3.0 and in some test project I\'m doing some custom drawing by overriding Node\'s draw method, but in the DrawPrimitives exam

4条回答
  •  执念已碎
    2020-12-29 00:46

    There is sample on cocos2d-x RC0 package that shows how to use the DrawPrimitives on top of other layers.

    On your Layer .h add the following:

    private:
        void onDrawPrimitives(const kmMat4 &transform, bool transformUpdated);
        CustomCommand _customCommand;
    

    Now in the cpp of the Layer, override the layer draw method and include the onDrawPrimitives method:

    void MyLayer::onDrawPrimitives(const kmMat4 &transform, bool transformUpdated)
    {
        kmGLPushMatrix();
        kmGLLoadMatrix(&transform);
    
        //add your primitive drawing code here
        DrawPrimitives::drawLine(ccp(0,0), ccp(100, 100));
    }
    
    void MyLayer::draw(Renderer *renderer, const kmMat4& transform, bool transformUpdated)
    {
        _customCommand.init(_globalZOrder);
        _customCommand.func = CC_CALLBACK_0(MyLayer::onDrawPrimitives, this, transform, transformUpdated);
        renderer->addCommand(&_customCommand);
    }
    

提交回复
热议问题