0

配列からアイテムを削除する際に問題があります..

最初: zan と呼ばれる int 型の変数があります。私の 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;
}

私のセレクターでは、変数をインクリメントします。

-(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 ( == ) のロジックにはあまり従いませんが、 loop の代わりにすべてのオブジェクトを削除しようとしている場合:

[_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 に答える