2014-07-21 47 views
0

我試圖把countDown放在我的應用程序上。我正在使用SKLabelNode。這是我的代碼:在swift和spritekit倒計時

class PlayScene: SKScene { 
    var startCount = true 
    var setTime = 0 
    var myTime = 0 
    var printTime = SKLabelNode(fontNamed: "arial") 
    override func didMoveToView(view: SKView!) { 
     self.backgroundColor = UIColor(hex: 0xD64541) 
     self.printTime.text = NSString(format: "%i", self.myTime) 
     self.addChild(self.printTime) 
    } 
    override func update(currentTime: NSTimeInterval) { 
     if self.startCount == true { 
      self.setTime = Int(currentTime) 
      self.startCount = false 
     } 
     self.myTime = Int(currentTime) - self.setTime 
     } 
    } 

我沒有問題,編譯它,但它崩潰的執行... 我認爲這是從我的字符串皈依未來..

謝謝

回答

0
float timeforFunction; 
    int numberoftime; 
    int __totalscore; 
    timeforFunction=1.5f; 
    numberoftime=10; 

    SKAction *updateTime= [SKAction sequence:@[ 
               [SKAction waitForDuration: timeforFunction], 
               [SKAction performSelector:@selector(updatescore) 
                   onTarget:self] 

               ]]; 
    [self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"]; 


    -(void) updatescore 
{ 
    __totalscore+=1; 
    _GamescoreText.text = [NSString stringWithFormat:@「%i」, __totalscore]; 
} 

,如果你想永遠重複這個動作只是改變

[self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"]; 
+2

謝謝你的回答,但這並不是我真正期待的。其實我的方法工作:myTime正確更新。我的問題是關於如何使用sklabelnode打印它。此外,我編碼在迅速不客觀的C :) – Quentin

2

I H這有助於。你幾乎已經有了它

class PlayScene: SKScene { 

var startCount = true 
var setTime = 0 
var myTime = 0 
var printTime = SKLabelNode(fontNamed: "arial") 

override func didMoveToView(view: SKView) { 
    self.backgroundColor = UIColor.orangeColor() 

    //self.printTime.text = NSString(format: "%i", self.myTime)//you need the position 
    self.printTime.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5) 
    self.addChild(self.printTime) 
} 
override func update(currentTime: NSTimeInterval) { 
    if self.startCount == true { 
     self.setTime = Int(currentTime) 
     self.startCount = false 
    } 
    self.myTime = Int(currentTime) - self.setTime 
    self.printTime.text = ("format:\(self.myTime)")//put your label in update so it shows each second. 
} 
2

下面是我爲我的Swift/SpriteKit'Breakout'應用程序解決了這個問題。我想在主遊戲屏幕上從5點到1點倒計時,但在球開始移動之前。我添加了這些功能,然後在didMoveToView結尾處撥打倒計時(5):請注意ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))endCountdown的最後一步,它啓動球並有效啓動遊戲。

func countdown(count: Int) { 
    countdownLabel.horizontalAlignmentMode = .Center 
    countdownLabel.verticalAlignmentMode = .Baseline 
    countdownLabel.position = CGPoint(x: size.width/2, y: size.height*(1/3)) 
    countdownLabel.fontColor = SKColor.whiteColor() 
    countdownLabel.fontSize = size.height/30 
    countdownLabel.zPosition = 100 
    countdownLabel.text = "Launching ball in \(count)..." 

addChild(countdownLabel) 

let counterDecrement = SKAction.sequence([SKAction.waitForDuration(1.0), 
    SKAction.runBlock(countdownAction)]) 

runAction(SKAction.sequence([SKAction.repeatAction(counterDecrement, count: 5), 
    SKAction.runBlock(endCountdown)])) 

} 

func countdownAction() { 
    count-- 
    countdownLabel.text = "Launching ball in \(count)..." 
} 

func endCountdown() { 
    countdownLabel.removeFromParent() 
    ball.physicsBody!.applyImpulse(CGVectorMake(20, 20)) 
} 
+0

這對我來說很好,我會明確說明什麼變量,你必須初始化在文件的頂部爲了這個工作,例如我補充說: (var countdownLabel = SKLabelNode(),var countdownCount:Int = 3)頂部。最後,爲了防止錯誤,我發現它有助於添加一個Bool來指示倒計時正在運行,因此無法意外觸發兩次(var countdownRunning:Bool = false) – rrphenix