Problem with a helloworld game, thanks for coming inside.

Attached the Classes files.

GameScene.h
class Game : public cocos2d::CCLayer
{
public:
cocos2d::CCSprite* player;
cocos2d::CCPoint playerVelocity;
cocos2d::CCArray* spiders;// the problem is about this array
float spiderMoveDuration;
int numSpidersMoved;
bool init();
void initSpiders();
void resetSpiders();
void spidersUpdate(float delta);
void runSpiderMoveSequence(cocos2d::CCSprite* spider);
void spiderBelowScreen(CCObject* spider);
virtual void update(float delta);
static cocos2d::CCScene* scene();
void didAccelerate(cocos2d::CCAcceleration **pAccelerationValue);
CREATE_FUNC;
};
—————————————————

void Game::resetSpiders
{
CCSize screenSize = CCDirector::sharedDirector~~>getWinSize;
CCSprite* tempSpider = spiders~~>lastObject;
CCSize size = tempSpider~~>getTexture~~>getContentSize;
int numSpiders = spiders~~>count;
for {
CCSprite* spider = spiders~~>objectAtIndex;
spider~~>setPosition.height/2));
spider~~>stopAllActions;
}
this~~>unschedule);
this~~>schedule, 0.7f);
numSpidersMoved = 0;
spiderMoveDuration = 4.0f;
CCLog;
CCLog);
}
void Game::spidersUpdate
{
CCLog; // address value here is same as the output in resetSpiders; but…
CCLog); // when I want to retrieve the count, the value is wrong. look the pointer has been changed.
// for
// {
// float random = CCRANDOM_0_1;
// int randomSpiderIndex = random** spiders~~>count;
// CCSprite* spider = spiders~~>objectAtIndex(randomSpiderIndex);
// CCLog(“%d random spider index”, randomSpiderIndex);
// if (spider~~>numberOfRunningActions == 0)
// {
// this~~>runSpiderMoveSequence(spider);
// break;
// }
// }
}


Classes.zip (5.0 KB)

So where is your question? You posted the code but you did not say what is the problem. I’m guessing that you wanted to ask why you get a crash after uncommenting code from your spidersUpdate method. The answer to that question would be because this CCArray object: spiders = CCArray::createWithCapacity(numSpiders); is an autorelese object and it will be automatically deleted after some time. Here is a short tutorial about memory management in cocos2d-x http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x

Thank you so much, this is exactly what I need.

Leszek Leszek wrote:

So where is your question? You posted the code but you did not say what is the problem. I’m guessing that you wanted to ask why you get a crash after uncommenting code from your spidersUpdate method. The answer to that question would be because this CCArray object: spiders = CCArray::createWithCapacity(numSpiders); is an autorelese object and it will be automatically deleted after some time. Here is a short tutorial about memory management in cocos2d-x http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Reference_Count_and_AutoReleasePool_in_Cocos2d-x