Search code examples
iosobjective-csprite-kitskspritenode

Keeping track of SKNodes for a category


I have a function that adds enemies to my scene that get called by an timing interval

How can I keep track of the number of enemies on the Scene to limit the amount of enemies I have in each level?

**In my update function **

CFTimeInterval timeSinceLastEnemy = currentTime - self.lastEnemyUpdateTime;
self.lastEnemyUpdateTime = currentTime;
if (timeSinceLastEnemy > 1) { // more than a second since last update
    timeSinceLastEnemy = 1.0 / 60.0;
    self.lastEnemyUpdateTime = currentTime;
}

[self spwanEnemyWithTime:timeSinceLastEnemy];

**The timer and The method to add enemies **

- (void)spwanEnemyWithTime:(CFTimeInterval)timeSinceLast {

    self.lastEnemySpawn += timeSinceLast;
    if (self.lastEnemySpawn > 0.6) {
        self.lastEnemySpawn = 0;
        [self spawnEnemy];
    }
}

-(void) spawnEnemy {

    SKSpriteNode *enemy = [SKSpriteNode spriteNodeWithImageNamed: enemySprite];

    int minX = 5;
    int maxX = self.frame.size.width;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;

    // Create the enemy slightly off-screen along the upper edge,

    enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(enemy.size.height - 10 , enemy.size.width)]; 
    enemy.physicsBody.dynamic = YES;
    enemy.physicsBody.categoryBitMask = enemyCategory; 
    enemy.physicsBody.contactTestBitMask = bulletCategory; 
    enemy.physicsBody.collisionBitMask = 0;     

    // and along a random position along the X axis as calculated above
    enemy.position = CGPointMake(actualX, self.frame.size.height + enemy.size.height);
    [self addChild:enemy];
    enemy.xScale = 0.2;
    enemy.yScale = 0.2;
    enemy.zPosition = 4;

    // Create the actions
    SKAction * actionMove = [SKAction moveToY:(0 - enemy.size.height) duration:4];
    SKAction * actionMoveDone = [SKAction removeFromParent];
    [enemy runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}

Solution

  • I confess I couldn't grasp what you are doing with the double timers, so I will assume that portion works as you want it to. for the enemy count couldn't this be as simple as incrementing a counter on your spawnEnemy and decrementing the counter when the enemy removes itself from the scene?

    You'll have to double check my Obj-c I haven't used it in a while.

    int enemyCount = 0;
    
    -(void) spawnEnemy {
    
        enemyCount += 1
    
        ...
    
        // Create the actions
        SKAction *actionMove = [SKAction moveToY:(0 - enemy.size.height) duration:4];
        SKAction *actionMoveDone = [SKAction removeFromParent];
        SKAction *decrementCount = [SKAction runBlock: ^(void) {
            enemyCount -= 1;                           
        }];
    
        [enemy runAction:[SKAction sequence:@[actionMove, actionMoveDone, decrementCount]]];
    }
    

    FYI you have a typo in your method "spwanEnemyWithTime"