Cocos2d断言失败:引用计数应大于0

时间:2016-12-15 19:38:41

标签: cocos2d-x sprite game-engine

我试图使用touchBegan在另一个中添加一个公共节点,但是我通过控制台

得到了这个错误
Assert failed: reference count should be greater than 0
Assertion failed: (_referenceCount > 0), function retain, file /Users/user/Desktop/App/cocos2d/cocos/base/CCRef.cpp, line 93.

这是我的代码:

·H

cocos2d::Node* node1 = cocos2d::Node::create();
cocos2d::Node* node2 = cocos2d::Node::create();

.ccp

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
 node1->removeAllChildren();
 node1->addChild(node2);
return true
}

2 个答案:

答案 0 :(得分:1)

你在.h文件中创建节点?那不行。在init功能中执行:

bool HelloWorld::init()
{
    if (!Layer::init())
    {
        return false;
    }

    auto node1 = Node::create();
    auto node2 = Node::create();
}

答案 1 :(得分:0)

·H

class HelloWorld
{
...

Node *node1;
Node *node2;
}

的.cpp

bool HelloWorld::init()
{
    if (!Layer::init())
    {
        return false;
    }

   node1 = Node::create();
   addChild(node1);
}

bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
 node1->removeAllChildren();
 node2 = Node::create();
 node1->addChild(node2);
return true
}