2

画面上部からランダムな障害物が落ちてきて、プレイヤーがそれらを避けなければならないゲームを作っています。画面下にあるノード(障害物)を削除する方法の実装に困っています。単純に見えるかもしれませんが、障害物を生成する方法がわかりにくくなっています。障害物を生成する方法は次のとおりです。

- (void)spawnNewObstacles
{
    //spawn obstacles
    SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"black.png"];

    int position = arc4random() % 320 + 1;
    obstacle.position = CGPointMake(position, 800);
    obstacle.size = CGSizeMake(200, 20);

    obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.frame.size];
    obstacle.physicsBody.dynamic = YES;

    [self addChild:obstacle];
    [obstacles addObject:obstacle]; //obstacles is an NSMutableArray

    [obstacle.physicsBody applyImpulse:CGVectorMake(0, -80.0f)];

    [self performSelector:@selector(spawnNewObstacles) withObject:nil afterDelay:0.25];
}

そして、画面の下にあるものを削除しようとしている(しかし失敗している)方法は次のとおりです。

-(void)update:(CFTimeInterval)currentTime
{ 
    for(int i=0;i<obstacles.count;i++)
    {
        NSLog(@"Removed an Obstacle"); //this log isn't showing so i concluded that it wasn't working
        SKSpriteNode *obstacle = [obstacles objectAtIndex:i];
        if(obstacle.position.y<0)
        {
            [obstacle removeFromParent];
        }
    }
}

どうすれば私はこれを行うことをお勧めしますか?

ps plzはコードで説明します...

4

1 に答える 1