【问题标题】:How to Spawn Sprites with Increasing Speed如何以越来越快的速度生成精灵
【发布时间】:2014-07-05 08:37:21
【问题描述】:

我想知道如何以逐渐增加的速度连续生成精灵。我确实看到了“如何逐渐增加某物的速率?”的问题。但我不确定如何将它应用到我的代码中。此外,如果有比使用游戏计时器更好的定期生成精灵的方法,请告诉我。

我尝试使用循环并增加“scheduledTimerWithTimeInterval”,但它最终导致同时产生大量精灵。

-(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];


}

感谢您的帮助。

【问题讨论】:

    标签: sprite-kit timing


    【解决方案1】:

    使用非重复的 NSTimer。此外,在计时器调用的方法 (getBalls) 中,调用计时器并降低间隔率(线性、指数,随心所欲)。


    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];
    
        }
    }
    

    【讨论】:

    • 谢谢!这非常有效(几乎)。我希望 decTime 无限下降,所以我尝试这样做,if (decTime > -1) { //linear increase decTime *= .1f; gameTimer = [NSTimer scheduledTimerWithTimeInterval:decTime target:self selector:@selector(getBalls) userInfo:nil repeats:NO]; } 这是一个正确的方法吗,这样数字会继续自己除,而不是最终减去循环停止的条件?
    • 你必须自己去发现,我帮了你这么远。
    最近更新 更多