0
votes

I want to know how to continuously spawn sprites with gradually increasing speed. I did see the question "How to gradually increase the rate of something?" but I'm not sure how to apply it to my code. Additionally, if there is a better way to spawn sprites regularly than using a game timer, please let me know.

I tried using a loop and having the "scheduledTimerWithTimeInterval" increase, but it ended up just causing numerous sprites to spawn at the same time.

-(void)getBalls {


//[userScore.node removeFromParent];

SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

int x = arc4random() %320;

CGPoint myPoint = CGPointMake(x, 480);
ball.position = myPoint;

ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

ball.physicsBody.categoryBitMask = ballCategory;

ball.physicsBody.contactTestBitMask = paddleCategory;

ball.physicsBody.collisionBitMask = ballCategory;

[self addChild:ball];

SKLabelNode *userScore = [SKLabelNode labelNodeWithFontNamed:@"Impact"];
userScore.fontColor = [SKColor blackColor];
userScore.text = [NSString stringWithFormat:@"SCORE: %i", score];
userScore.fontSize = 18;
userScore.position = CGPointMake(CGRectGetMidX(self.frame) + 110, CGRectGetHeight(self.frame)            
- 30);
[self addChild:userScore];
[userScore runAction:[SKAction fadeAlphaTo:1.0 duration:1.5] completion:^{
    [userScore removeFromParent];
}];




}

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor  whiteColor];


self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;

gameTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self      
selector:@selector(getBalls) userInfo:nil repeats:YES];

[self addPlayer:size];


}

Thanks for your help.

1

1 Answers

0
votes

Use a non-repeating NSTimer. Also, in the method called by the timer (getBalls) recall the timer and decrease the interval rate (linearly, exponentially, however you want).


float decTime;
decTime = 1.5f;

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
    self.backgroundColor = [SKColor  whiteColor];

    self.physicsWorld.gravity = CGVectorMake(0, -9.8);
    self.physicsWorld.contactDelegate = self;

    gameTimer = [NSTimer scheduledTimerWithTimeInterval:decTime target:self      
    selector:@selector(getBalls) userInfo:nil repeats:NO];

    [self addPlayer:size];

}

-(void)getBalls {


    //[userScore.node removeFromParent];

    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];

    int x = arc4random() %320;

    CGPoint myPoint = CGPointMake(x, 480);
    ball.position = myPoint;

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.frame.size.width/2];

    ball.physicsBody.categoryBitMask = ballCategory;

    ball.physicsBody.contactTestBitMask = paddleCategory;

    ball.physicsBody.collisionBitMask = ballCategory;

    [self addChild:ball];

    ....

    if (decTime > .05f) {

        //linear increase
        decTime -= .01f;

        gameTimer = [NSTimer scheduledTimerWithTimeInterval:decTime target:self      
        selector:@selector(getBalls) userInfo:nil repeats:NO];

    }
}