我已经创建了一个 no of sprite 并以相同的名称将它们添加到场景中。
现在,我想删除所有这些,甚至是那些没有任何引用的。我怎样才能做到这一点。
不使用 removeChildBytag();
或不使用标签。
void Science::onBallMovement(cocos2d::Ref* pSender){
auto sp = (Sprite*)pSender;
v1 = sp->getPosition();
if (v1 == v2) {
return;
}
if(v1!=v2) {
if (sp->getTag()==1) {
s = Sprite::create("Dot.png");
}
if (sp->getTag()==2) {
s = Sprite::create("BlueDot.png");
}
s->setPosition(v1);
s->autorelease();
this->addChild(s);
}
v2 = v1;
}
void Science::update(float delta){
if (setting) {
this->onBallMovement(spr);
this->onBallMovement(spr1);
}
}
通过覆盖节点类的更新方法,我重复调用上面的方法,并根据随机移动的 Sprite 的当前位置,创建路径跟踪器。但是现在,我想根据用户选择将其删除,我该怎么做?
最佳答案
有多种方法可以实现这一点,所以我将解释最简单的方法:
初始化全局节点并删除所有子节点
Declare a node in the .h file of your class.
Node* _spriteParent;
Now define the node in the init method of your class.
_spriteParent = Node::create();
_spriteParent->setPosition(Vec2::ZERO);
this->addchild(_spriteParent);
Now whenever you are creating the sprite, just add that sprite to the above node, which you have just created(do not call autorelease on the sprite).
_spriteParent->addChild(s);
当你想删除所有你已经添加到_spriteParent的 Sprite 时,调用这个方法:
_spriteParent->removeAllChildren();
你就完成了。希望对您有所帮助。
关于c++ - 我已经创建了一个 Sprite 并使用相同的引用将它们添加到场景中,现在,我如何在不使用标签的情况下删除它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37919860/