我想知道如何以逐渐增加的速度连续产生精灵。我确实看到了“如何逐步提高某些比率?”的问题。但我不知道如何将它应用到我的代码中。此外,如果有比使用游戏计时器更好的方法来定期生成精灵,请告诉我。
我尝试使用循环并使“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];
}
感谢您的帮助。
答案 0 :(得分:0)
使用非重复的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];
}
}