我在我的代码中添加了一个drawNode子项并使用一个子项绘制了许多点。然后我如何删除pointNode的点形式,只需删除point并将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]
答案 0 :(得分:0)
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;
}
}
};
};