Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 对子弹进行适当的垃圾收集_Iphone_Xcode_Cocos2d Iphone - Fatal编程技术网

Iphone 对子弹进行适当的垃圾收集

Iphone 对子弹进行适当的垃圾收集,iphone,xcode,cocos2d-iphone,Iphone,Xcode,Cocos2d Iphone,所以我正在为一个游戏使用一个简单的子弹系统,但我想知道什么是从阵列和屏幕上移除子弹的最好方法,这样就不会占用帧速率和内存了 -(void)spinTapped { [self.character stopAllActions]; [self.character runAction:self.gunAction]; isRunning = NO; CCSprite *bullet = [CCSprite spriteWith

所以我正在为一个游戏使用一个简单的子弹系统,但我想知道什么是从阵列和屏幕上移除子弹的最好方法,这样就不会占用帧速率和内存了

-(void)spinTapped
{     
        [self.character stopAllActions];
        [self.character runAction:self.gunAction];
        isRunning = NO;
        CCSprite *bullet = [CCSprite spriteWithFile:@"rwby_bullet.png"];
        bullet.position = ccp(self.character.position.x , self.character.position.y + 15);
        [bullet setScale:2];
        if (isRight) {
            bullet.tag = 10;
        }
        else {
            bullet.tag = -10;
        }
        [bullets addObject:bullet];
        [self addChild:bullet z:-1];

}
然后在更新中:

for(CCSprite *bullet in bullets)
    {
        CGPoint bulletPosition = ccp(bullet.position.x , bullet.position.y);
        CGPoint B_tilePosition = [self tileCoorForPosition:bulletPosition];
        bullet.position = ccp(bullet.position.x + bullet.tag , bullet.position.y);

        NSMutableArray *emptySpace = [[NSMutableArray alloc] initWithCapacity:10000];
        [emptySpace addObject:[NSNumber numberWithInt:0]];

        @try {
            bullet_GID = [self.background tileGIDAt:B_tilePosition];
        }
        @catch (NSException *exception) {
            bullet_GID = 535677655;
        }
        @finally {

        }
        if(bullet_GID == 535677655)
        {
            [bullet setVisible:NO];
          //  [bullets removeObject:bullet];

        }
        else if(bullet_GID)
        {
            [bullet setVisible:NO];
          //  [bullets removeObject:bullet];
        }
    }

[bullets removeObject:bullet]会导致应用程序在一颗子弹击中一个磁贴而另一颗子弹出现在屏幕上时崩溃(我认为问题出在这里)。那么,删除项目符号的正确方法是什么?

崩溃可能是因为您试图在“迭代数组时”从项目符号数组中删除项目符号对象:在迭代数组时无法修改它。所以,我建议你做一些类似的事情

NSMutableArray *bulletsToRemove = [NSMutableArray array];

for (CCSprite *bullet in bullets) {
   // your logic 
   if('should remove bullet') {
       [bulletsToRemove addObject:bullet];
   }
}

// now iterate the bullets to remove array, and remove safely from
// the bullets array

for (CCSprite *bulletToRemove in bulletsToRemove) {
    [bullets removeObject:bulletToRemove];
}

它说bullet是一个未声明的标识符,这很有意义,因为我没有在bulletsToRemove中声明它。我应该在哪里申报?对不起,编辑了我的答案!在第二个循环中,我使用了错误的变量名;这更有道理!但是,当它运行并突出显示bulletsToRemove for循环作为原因时,它就会崩溃?使用[bullets ReverseObject Enumerator],然后允许删除枚举对象。这也会更快,因为removeObject:再次枚举数组,直到找到要删除的对象。