Search code examples
cocos2d-xcocos2d-x-3.0

Initialized property CCArray is still NULL after new CCArray


I have in scene class property in private section like

private:
CCArray* objects;

and in init I initialize

objects = new CCArray();

but when I put breakpoint in line below objects is NULL and I don't know why. It crashes when I call objects->count(); Why I cannot initialize property ?


Solution

  • you have tagged this post with cocos2d-x 3.0. Array has changed in cocos2d-x-3.0.

    Initialize :

    cocos2d::Vector<cocos2d::Sprite *> _bullets;
    

    Populate:

    //   add a bullet
    Sprite *bullet = Sprite::create("circle.png")
    this->_bullets.pushBack(bullet);  //  retains bullet
    

    Looping:

    //  loop through bullets
    for (auto bullet: this->_bullets)
    {
        //  do something with bullet.
        //  no need to cast in this case
        if (bullet->getPositionX() > 160)
        {
            //  ...
        }
    }
    

    Erasing :

    this->_bullets->removeObject(bullet);
    

    You can read all about it here: http://dev.bunnyhero.org/2014/01/cocos2d-x-30-beta-the-new-vector-class/