如何为 iPhone 游戏创建功能性 HUD?
How to create a functional HUD for iPhone game?
我正在创建一个迷你 iOS 游戏来尝试 Objective C。无论如何,我有一个工作原型和一个有点功能的 HUD。它显示了分数、行进的距离和玩家之前的最高分。但是,我无法实现玩家还剩多少生命。有人可以帮助我或为我指明正确的方向吗?任何帮助表示赞赏。预先感谢您的帮助。
这是我的生命代码和 HUD,如果需要我会提供更多。
- (void)startTheGame{
_lives = 3;
double curTime = CACurrentMediaTime();
_gameOverTime = curTime + 30.0;
_nextAsteroidSpawn = 0;
_gameOver = NO;
for (SKSpriteNode *asteroid in _asteroids) {
asteroid.hidden = YES;
}
for (SKSpriteNode *laser in _shipLasers) {
laser.hidden = YES;
}
_ship.hidden = NO;
//reset ship position for new game
_ship.position = CGPointMake(self.frame.size.width * 0.1, CGRectGetMidY(self.frame));
//setup to handle accelerometer readings using CoreMotion Framework
[self startMonitoringAcceleration];
_highscore.text = [NSString stringWithFormat:@"High: %li pt", [RWGameData sharedGameData].highScore];
_score.text = @"0 pt";
_distance.text = @"";
lives.text = @" 3";
}
HUD:
-(void)setupHUD{
_score = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_score.fontSize = 12.0;
_score.position = CGPointMake(50, 7);
_score.fontColor = [SKColor blackColor];
[self addChild:_score];
_distance = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_distance.fontSize = 12.0;
_distance.position = CGPointMake(115, 7);
_distance.fontColor = [SKColor cyanColor];
[self addChild:_distance];
_highscore = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_highscore.fontSize = 12.0;
_highscore.position = CGPointMake(200, 7);
_highscore.fontColor = [SKColor redColor];
[self addChild:_highscore];
lives = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
lives.fontSize = 12.0;
lives.position = CGPointMake(250, 7);
lives.fontColor = [SKColor yellowColor];
[self addChild:_lives];
}
很难用你发布的代码来判断,但我确实注意到你使用了 lives = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
然后添加了 [self addChild:_lives];
在您的 startTheGame 方法中,您将 3 分配给 _lives
这显然不能用作 SKLabelNode。
如果这不能解决问题,请仔细检查您是否将 lives
声明为 SKLabelNode 而不是其他内容。
如果_lives
是int类型,livesLabel
是SKLabelNode类型,需要时可以按如下方式设置标签节点的文本
livesLabel.text = [NSString stringWithFormat:@"Lives: %d",_lives];
同样在最后一行中,您将 _lives
添加到 self,它是一个 int 或 NSInteger(无论您将其定义为什么)。应该是 livesLabel
[self addChild:livesLabel];
我正在创建一个迷你 iOS 游戏来尝试 Objective C。无论如何,我有一个工作原型和一个有点功能的 HUD。它显示了分数、行进的距离和玩家之前的最高分。但是,我无法实现玩家还剩多少生命。有人可以帮助我或为我指明正确的方向吗?任何帮助表示赞赏。预先感谢您的帮助。
这是我的生命代码和 HUD,如果需要我会提供更多。
- (void)startTheGame{
_lives = 3;
double curTime = CACurrentMediaTime();
_gameOverTime = curTime + 30.0;
_nextAsteroidSpawn = 0;
_gameOver = NO;
for (SKSpriteNode *asteroid in _asteroids) {
asteroid.hidden = YES;
}
for (SKSpriteNode *laser in _shipLasers) {
laser.hidden = YES;
}
_ship.hidden = NO;
//reset ship position for new game
_ship.position = CGPointMake(self.frame.size.width * 0.1, CGRectGetMidY(self.frame));
//setup to handle accelerometer readings using CoreMotion Framework
[self startMonitoringAcceleration];
_highscore.text = [NSString stringWithFormat:@"High: %li pt", [RWGameData sharedGameData].highScore];
_score.text = @"0 pt";
_distance.text = @"";
lives.text = @" 3";
}
HUD:
-(void)setupHUD{
_score = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_score.fontSize = 12.0;
_score.position = CGPointMake(50, 7);
_score.fontColor = [SKColor blackColor];
[self addChild:_score];
_distance = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_distance.fontSize = 12.0;
_distance.position = CGPointMake(115, 7);
_distance.fontColor = [SKColor cyanColor];
[self addChild:_distance];
_highscore = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
_highscore.fontSize = 12.0;
_highscore.position = CGPointMake(200, 7);
_highscore.fontColor = [SKColor redColor];
[self addChild:_highscore];
lives = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
lives.fontSize = 12.0;
lives.position = CGPointMake(250, 7);
lives.fontColor = [SKColor yellowColor];
[self addChild:_lives];
}
很难用你发布的代码来判断,但我确实注意到你使用了 lives = [[SKLabelNode alloc] initWithFontNamed:@"Futura-CondensedMedium"];
然后添加了 [self addChild:_lives];
在您的 startTheGame 方法中,您将 3 分配给 _lives
这显然不能用作 SKLabelNode。
如果这不能解决问题,请仔细检查您是否将 lives
声明为 SKLabelNode 而不是其他内容。
如果_lives
是int类型,livesLabel
是SKLabelNode类型,需要时可以按如下方式设置标签节点的文本
livesLabel.text = [NSString stringWithFormat:@"Lives: %d",_lives];
同样在最后一行中,您将 _lives
添加到 self,它是一个 int 或 NSInteger(无论您将其定义为什么)。应该是 livesLabel
[self addChild:livesLabel];