c++ - 我已经创建了一个 Sprite 并使用相同的引用将它们添加到场景中,现在,我如何在不使用标签的情况下删除它们?

标签 c++ cocos2d-iphone cross-platform cocos2d-x sprite

我已经创建了一个 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/

相关文章:

c++ - 删除 char 类型的 un-/signedness

assembly - 跨平台/架构汇编语言

c++ - 从 ‘const char*’ 到 ‘char’ 类错误的无效转换

c++ - 使用 native 依赖项加载 clr 库

xcode - 如何将我的 .m 文件更改为 .mm 文件?

ios - 如何定位 CCParticleFire,使其始终与移动 Sprite 处于同一位置?

android - cocos2d-x 未定义引用

compiler-errors - 无法交叉编译gdb/gdbserver

java - 为什么 Java 类加载在 Linux 上失败,但在 Windows 上成功?

c++ - 如何打印二维对 vector 的值?