如何在 cocos2dx 中从 DrawNode 中删除色点,但我不想从父级中删除孩子。只需删除它的某些点

how can I remove the color point from DrawNode in cocos2dx, but I don't want to removeChild from parent. Just remove some point of it

本文关键字:删除 孩子 中从 cocos2dx DrawNode 我不想      更新时间:2023-10-16

我在代码中添加了一个drawNode子项,并使用一个子项绘制多个点。那么我怎样才能从绘图节点中删除点,只需删除点并将绘图节点保留在这里。

    auto m_pDrawPoint = DrawNode::create();
    this->addChild(m_pDrawPoint);                   
    for (int i = 0; i < 10; i++)
    {
      m_pDrawPoint->drawPoint(screenP[i], 20, Color4F::GREEN);
    }
     // I  want remove some of  point Like remove the screenP[3] 

cocos2d-x 中没有clearPoint。调用DrawNode::drawPoint时,drawNode 只需将位置、点大小和颜色保存在裸数组上。而且,除非您覆盖DrawNode,否则您无权访问此数组。如果要清除某些点,最好使用DrawNode::clear删除点,然后重新绘制所需的点。

//编辑

    auto m_pDrawPoint = DrawNode::create();
    this->addChild(m_pDrawPoint);                   
    for (int i = 0; i < 10; i++)
    {
      m_pDrawPoint->drawPoint(screenP[i], 20, Color4F::GREEN);
      m_points.push_back(screenP[i]);
    }
void Foo::removePoint(const Vec2& point){
    for(int i=0; i<=m_points.size(); i++){
        if(point==m_points[i]){
            //this is a trick
            m_points[i] = m_points[m_points.size()];
            m_points.pop_back();
            break;
        }
    }
    m_pDrawPoint.drawPoints(m_points.data(), m_points.size(),20, Color4F::GREEN);
}

子类化DrawNode似乎更简单。

class MyDrawNode: public DrawNode{
public:
    void removePoint(const Vec2& point){
        for(int i=0; i<_bufferCountGLPoint; i++){
            V2F_C4B_T2F *p = (V2F_C4B_T2F*)(_bufferGLPoint + i);
            // point==p->vertices doesn't work as expected sometimes.
            if( (point - p->vertices).lengthSquared() < 0.0001f ){
                *p = _bufferGLPoint + _bufferCountGLPoint - 1;
                _bufferCountGLPoint++;
                break;
            }
        }
    };
};