1
votes

A friends and me are developing a game for Iphone with cocos2dx. It is simple, the tipical runner in a infinite map with random obstacles.

The map is charged in chuncks, all of this initializated with random structures, when the position of the player is in the end of the chunck add the next chunck, and if the player is in the start of the chunck remove the chunck passed.

In the scene, I have a listener called by this way:

//For remove a chunck from the view
unsigned int id = this->getChunckForRemove();
this->_listener->removeChunckFromScene(id);
....
//For add a chunck to the view
TiledMap::Chunck* chunck = new TiledMap::Chunck(this.globalPosition);
this->globalPosition += SIZE_OF_CHUNCK; //Ex.: SIZE_OF_CHUNCK = 5194
this->_listener->addChunckToScene(chunck, 1);

The implemenentation is the scene, and it the next:

void
Scenes::
PlayerTestScene::removeChunckFromScene(const int id)
{
    log("Remove chunck from scene :%d", id);
    this->_nodeScroll->removeChildByTag(id);
}
...
void
Scenes::
PlayerTestScene::addChunckToScene(const int id, TiledMap::Chunck* chunck)
{
    log("Add chunck to scene :%d", id);
    this->_nodeScroll->addChild(chunck->_node, 1,  id);
    this->player->setFloorCollision(chunck->_collisionables);
}

The structure Chunck is the next:

class Chunck {
  Node* _node;
  std::vector<BlockCollisionable> _collisionables;
  ....
  Chunck() {
     _node = Node::create();
     _node->retain();
  }
  ~Chunck() {
     _node->autorelease();
  }
}

The problem is when I add the chunck, I have no problem in the logs but it not drawed. The screen in these position are black, I don't undertand what it is hapening.

In addition, When I remove the last chunck fail with this error:

Assert failed: reference count should be greater than 0 Assertion failed: (_referenceCount > 0), function release, file /.../cocos2d/cocos/base/CCRef.cpp, line 98.

Thanks :D

1

1 Answers

1
votes

You cannot see Node because you create just logic node and there's nothing to draw. It is not a Sprite or LayerColor or TextField.

Next thing - when you call ::create for cocos class it automatically calls autorelease and automatically delete node when no one hold it and reference counter is zero. If you manually call retain you increase reference count by 1 and then you should manually call release (not autorelease) to decrease it and allow to remove.

When you add node to scene/layer/node via addChild it also will call retain and when you remove it from scene/layer/node via removeFromParent it'll call release