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 關注
- 815 瀏覽
添加回答
舉報
0/150
提交
取消