我在代码中添加了一个drawNode子元素,并使用一个子元素来绘制多个points.Then,如何从drawNode中删除点--只删除点,并将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]
发布于 2016-03-09 05:07:43
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;
}
}
};
};
https://stackoverflow.com/questions/35887618
复制