0

我有一个问题,从数组中删除项目..

首先:我有一个 int 类型的变量,叫做 zan。在我的 HelloWordScene.h

int zan;
NSMutableArray * targets;
NSMutableArray *projectiles;

在我的 HelloWordScene.m 中。我有一个对象,动画:

-(id) init {
    if((self = [super init])) {
        [self schedule:@selector(update:)];
        _targets = [[NSMutableArray alloc] init];
        _projectiles = [[NSMutableArray alloc] init];
    }
    [self schedule:@selector(gameLogic:) interval:3];
    return self;
}

在我的选择器中,我增加了 var:

-(void)gameLogic:(ccTime)dt {
    [self addTarget];
    zan ++;
}

后来我有一个动画,我有一个 addTarget:

// This loads an image of the same name (but ending in png), and goes through the
// plist to add definitions of each frame to the cache.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"zancudo.plist"];

// Create a sprite sheet with the images
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"zancudo.png"];
[self addChild:spriteSheet];

// Load up the frames of our animation
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 0; i <= 4; ++i) {
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"zancu000%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];

// Create a sprite for our bear
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:@"zancu0001.png"];



//random position
int minY = _bear.contentSize.width/2;
int maxY = winSize.width - _bear.contentSize.width/2;
int rangeY = maxY - minY;
int actualX = (arc4random() % rangeY) + minY;

// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
_bear.position = ccp(actualX,winSize.height + (_bear.contentSize.height/2));

self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];


//animation
[spriteSheet addChild:_bear];
//∫aqui pasar un for para poder saber que position es
CCLOG(@"cantidad de zancudos%d",zan);
[_targets insertObject:_bear atIndex:zan];

我已经为索引删除了一个可变的 _target - 我有一个选择器更新。尝试删除一个可变数组。

for (CCSprite *target in targetsToDelete) {
        if (_targets.count!=0) {
            for (int j=1; j==_targets.count; j++) {
                [_targets removeObjectAtIndex:j];
            }

        }

我需要帮助

4

3 回答 3

0

您的循环将尝试访问数组边界之外的索引...试试这个

for (int j = 0; j == _targets.count - 1; j++) 
{                 
     [_targets removeObjectAtIndex:j];             
}

但是,由于您似乎只是使用此代码删除数组的最后一个元素,因此您可以跳过 for 循环并使用:

[_targets removeLastObject];
于 2012-09-26T15:23:17.160 回答
0

从 _targets 中删除对象的那一刻,有效计数实际上会发生变化。因此,如果您以 10 的计数开始循环,最终 j 的值将超出范围。我没有很好地遵循您的 if ( == ) 的逻辑,但是如果您尝试删除所有对象,而不是循环:

[_targets removeAllObjects];
于 2012-09-26T15:32:49.613 回答
0

使用这样的语法进行无崩溃删除;

NSArray *array = [NSArray arrayWithArray: _targets];

for(CCSprite *sprite in array)
{
    if(sprite.tag == kToDelete) //mark somewhere in game :or use ur logic here
    {
        [_targets removeObject: sprite];
        [sprite stopAllActions];
        [sprite removeFromParentAndCleanup:YES];
    }
}
于 2012-09-27T10:56:59.037 回答