我使用Cocos2d-x进行我从Cocos2d-
iphone移植的游戏.最初的程序员似乎已经使用了Objective-C的“特性”来防止对nil对象的调用崩溃,这是一种做很多草率事情的方法.
但是,如果这与我不知道的相关,那么在我的代码中,我从不手动调用release(),当然也不会删除或类似的东西.我甚至根本没有调用 – > removeObject()(尽管这不会导致与我相同的问题).
现在的问题是:当游戏运行时,随机时刻(它们不会是随机的,但现在看起来很明显)子节点被设置为NULL.这不仅会影响我的代码,还会影响Cocos2d内部.例:
CCLog("----------------"); for(int j = 0; j < this->getChildren()->count(); j++) { CCObject *child = this->getChildren()->objectAtIndex(j); EnemySprite *enemy = dynamic_cast<EnemySprite*>(child); if (enemy != NULL) { CCLog("Enemy with tag %d found",enemy->getTag()); } } EnemySprite *enemy = dynamic_cast<EnemySprite*>(this->getChildByTag(i)); if (enemy == NULL) { CCLog("Now enemy with %d is NULL :(",i); }
在getChildren()外观中,所有带有标签的敌人都在那里并打印出来;