谢谢你花时间阅读这篇文章!
我在我的项目中使用了Cocos2d和Box2d。这个项目的逻辑非常简单。玩家只向enemies.If射击子弹击中敌人,子弹以及敌人将是destroyed.If任何敌人通过屏幕没有被击中,然后游戏结束。守则的部分内容如下:
- (void)birdDone:(ZLBird*) birdToDelete {
CCSprite *sprite = (CCSprite *)birdToDelete;
b2Body *spriteBody = NULL;
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *curSprite = (__bridge CCSprite *)b->GetUserData();
if (sprite == curSprite) {
spriteBody = b;
break;
}
}
}
if (spriteBody != NULL) {
world->DestroyBody(spriteBody);
}
[sprite removeFromParentAndCleanup:YES];
sprite = NULL;
}
-(void) update:(ccTime)delta{
SOME CODE HERE
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
CCSprite *myActor = (__bridge CCSprite*)b->GetUserData();
if (b->GetUserData() != NULL)
{
//Synchronize the AtlasSprites position and rotation with the corresponding body
myActor.position = (CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));
myActor.rotation = (-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
// goes through all the rocks
for (ZLRock *rockToAct in rocksArr){
[rockToAct incrementTime];
if ([rockToAct getExistedTime] >= 400)
{
// Remove the rock several seconds after it has been shot
[self rockDone:rockToAct];
}
}
// goes through all the elements in BirdsArr
for (ZLBird *birdToAct in birdsArr){
[birdToAct incrementTime];
if ([birdToAct getDeadTime] >= 400 || birdToAct.position.x > WIDTH_WINDOW+20)
{
// Remove the bird several seconds after it has been dead
[self birdDone:birdToAct];
}
// JUST FOR TEST
if (birdToAct.position.x > WIDTH_WINDOW && !birdToAct.isDead)
NSLog(@"YOU LOSSSSSSSS!");
}当我在我的iphone上运行这个项目时,不知怎么的,它变得很慢。我在仪器上检查了CPU的使用情况,CPU的使用率大约是70%。
为什么会这样呢?我想自从我启用ARC之后就没有内存泄漏了。这是因为我在每一帧中都要遍历几个数组,这真的会使设备慢下来吗?
谢谢你的帮忙!
发布于 2014-03-09 22:42:41
刚发现问题!
当我从它们的父对象中删除这些对象时,我没有从数组中删除它们。因此阵列的大小不断增大,最终导致挤压。
谢谢你的回答!
https://stackoverflow.com/questions/22280073
复制相似问题