SpriteKit-创建一个计时器如何创建一个计时器,它每隔两秒钟就会在我的屏幕上的HUD上增加一个分数?这是我对HUD的代码: @implementation MyScene{
int counter;
BOOL updateLabel;
SKLabelNode *counterLabel;}-(id)initWithSize:(CGSize)size{
if (self = [super initWithSize:size])
{
counter = 0;
updateLabel = false;
counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
counterLabel.name = @"myCounterLabel";
counterLabel.text = @"0";
counterLabel.fontSize = 20;
counterLabel.fontColor = [SKColor yellowColor];
counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
counterLabel.position = CGPointMake(50,50); // change x,y to location you want
counterLabel.zPosition = 900;
[self addChild: counterLabel];
}}
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
var timescore = Int() var actionwait = SKAction.waitForDuration(0.5) var timesecond = Int() var actionrun = SKAction.runBlock({ timescore++ timesecond++ if timesecond == 60 {timesecond = 0} scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)" }) scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
森林海
TA贡献2011条经验 获得超2个赞
func updateClock() { var leadingZero = "" var leadingZeroMin = "" var timeMin = Int() var actionwait = SKAction.waitForDuration(1.0) var timesecond = Int() var actionrun = SKAction.runBlock({ timeMin++ timesecond++ if timesecond == 60 {timesecond = 0} if timeMin / 60 <= 9 { leadingZeroMin = "0" } else { leadingZeroMin = "" } if timesecond <= 9 { leadingZero = "0" } else { leadingZero = "" } self.flyTimeText.text = "Flight Time [ \(leadingZeroMin)\(timeMin/60) : \(leadingZero)\(timesecond) ]" }) self.flyTimeText.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))}
- 3 回答
- 0 关注
- 777 浏览
添加回答
举报
0/150
提交
取消