我怎样才能在精灵上绘制一些不在精灵之后的东西

时间:2015-10-17 10:05:10

标签: cocos2d-x box2d draw cocos2d-x-3.0

我使用cocos2dx v3.8,并使用覆盖绘制成功绘制内容:

void Box2dManager::draw(Renderer *renderer, const Mat4& transform, uint32_t flags){
    GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION );
    Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    box2dWorld->DrawDebugData();
    Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
//  super::draw(renderer, transform, flags);
}

但它总是落后于精灵。修改z顺序后无法更改。

解决

现在我可以修复cocos2d v3.x box2d DrawDebugData问题了。 并且我粘贴了所有cocos2dx解决代码。 v2 :(简单,因为v2引擎opengl draw是每个单独的任务)

void Box2dManager::draw(){
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
    kmGLPushMatrix();
    box2dWorld->DrawDebugData();
    kmGLPopMatrix();
}

v3 :( v3引擎使用quence将draw commond推送到核心,所以你需要创建一个回调)

void Box2dManager::draw(Renderer *renderer, const Mat4& transform, uint32_t flags){
    super::draw(renderer, transform, _transformUpdated);
    GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
    Director* director = Director::getInstance();
    CCASSERT(nullptr != director, "Director is null when seting matrix stack");
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    _modelViewMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    _customCommand.init(_globalZOrder);
    _customCommand.func = CC_CALLBACK_0(Box2dManager::onDraw, this);
    renderer->addCommand(&_customCommand);
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}

void Box2dManager::onDraw() {
    Director* director = Director::getInstance();
    CCASSERT(nullptr != director, "Director is null when seting matrix stack");
    Mat4 oldMV;
    oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV);
    box2dWorld->DrawDebugData();
    director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV);
}

refrence: http://discuss.cocos2d-x.org/t/box2d-debug-drawing-for-cocos2d-x-3-0/11912/2 http://blog.csdn.net/zszeng/article/details/50000757

2 个答案:

答案 0 :(得分:0)

你如何使用DrawNode并将其作为孩子添加?如果要使用在绘图树上管理的节点,则更容易维护。

auto myDrawing = DrawNode::create();
myDrawing::drawRect(p1, p2, p3, Color4F(255, 255, 0, 255));
myDrawing::setLineWidth(2)
this->addChild(myDrawing); 

答案 1 :(得分:0)

    virtual void draw (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform,uint32_t flags) override;
   cocos2d::CustomCommand _customCmd;
 void   onDraw(const cocos2d::Mat4 &transform, uint32_t flags);



void linedraw:: draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags){
        //cocos2d::Layer::draw(renderer, transform, flags);


        _customCmd.init(1);
        _customCmd.func = CC_CALLBACK_0(linedraw::onDraw, this, transform, flags);
        renderer->addCommand(&_customCmd);

    }

        void  linedraw:: onDraw(const cocos2d::Mat4 &transform, uint32_t flags)
        {

            }