所以我正在为我正在开发的游戏使用一个简单的子弹系统,但我想知道从阵列和屏幕中移除子弹的最佳方法是什么,这样它就不会占用帧速率和内存?
-(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] 会导致应用程序在一颗子弹击中瓷砖而另一颗子弹在屏幕上时崩溃(我认为是问题所在)。那么什么是正确的去除子弹的方法呢?