2015-05-29 218 views
0

我刚开始学习如何为Apple设计游戏,并且一直在寻找如何停止计时器。在下面的代码中,我想停止计时器,但是我很难解决这个问题。我没有任何线索。请帮帮我!谢谢!如何停止计时器?

类游戏:SKScene {

var Ball = SKSpriteNode(imageNamed: "Red.png") 
var QuitOption = SKLabelNode() 
var ScoreLabel = SKLabelNode() 
var timescore = Int() 
var timesecond = Int() 
var locked = false 




override func didMoveToView(view: SKView) { 


    backgroundColor = SKColor.blackColor() // background for the display 

    self.physicsWorld.gravity = CGVectorMake(0, -9.8) 

    let SceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
    SceneBody.friction = 0 
    self.physicsBody = SceneBody 


    Ball.size = CGSize(width: 120, height: 120) 
    Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7) 
    Ball.physicsBody = SKPhysicsBody(circleOfRadius: 60) 
    Ball.physicsBody?.affectedByGravity = true 
    Ball.physicsBody?.restitution = 0.1 
    Ball.physicsBody?.linearDamping = 0 
    Ball.name = "Ball" 

    self.addChild(Ball) 

    QuitOption.text = "Quit" 
    QuitOption.fontName = "Noteworthy-Light" 
    QuitOption.fontColor = SKColor.greenColor() 
    QuitOption.fontSize = 35 
    QuitOption.position = CGPoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110) 
    QuitOption.name = "Quit" 

    addChild(QuitOption) 

    ScoreLabel = SKLabelNode(fontNamed: "Noteworthy-Light") 
    ScoreLabel.fontSize = 50     // The + will move it to the right side and - to the left side for more accuracy. 
    ScoreLabel.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/4 + 400) // position of ScoreLabelNode 
    ScoreLabel.name = "Score+" 
    ScoreLabel.hidden = false 

    self.addChild(ScoreLabel) 


} 

// Making the ball jump after user touches ball 

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 


    var touch = touches.first as! UITouch 
    var location = touch.previousLocationInNode(self) 
    // var location = touch.locationInNode(self) 
    var node = self.nodeAtPoint(location) 


    if (node.name == "Quit"){ 

     let myScene = GameScene(size: self.size) 
     myScene.scaleMode = scaleMode 
     let reveal = SKTransition.fadeWithDuration(1) 
     self.view?.presentScene(myScene, transition: reveal) 

    } 

    if (node.name == "Ball"){ 

     for touch: AnyObject in touches { 

      let location = touch.locationInNode(Ball) 

      Ball.physicsBody?.allowsRotation = true 
      Ball.physicsBody?.velocity = CGVectorMake(0, 0) 
      Ball.physicsBody?.applyImpulse(CGVectorMake(0, 100)) 

     } 


    } 

    if(!self.locked){ 

     self.locked = true 

    var actionrun = SKAction.waitForDuration(0.5) 

    var actionwait = SKAction.runBlock({ 

     var stopTimer = false 

     self.timescore++ 
     self.timesecond++ 


     if self.timesecond == 60 {self.timesecond = 0} 

     self.ScoreLabel.text = " \(self.timescore/60):\(self.timesecond)"}) 

    ScoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionrun,actionwait]))) 



    } 


} 

}

回答

1

不是100%肯定,如果我明白你的问题。 但是,如果“定时”指的是你的循环动作,你可以这样做:

// This is just running your action with a name "scoreAction" 
let loopAction = SKAction.repeatActionForever(
    SKAction.sequence([actionrun,actionwait])) 
ScoreLabel.runAction(loopAction, withKey: "scoreAction") 

在以后某个代码,你可以阻止这种行动:

ScoreLabel.removeActionForKey("scoreAction") 

UPDATE:

如果您只想暂停/取消暂停动画,则可以在要暂停的对象上使用paused属性。

ScoreLabel.pause = true 

很高兴知道.pause会影响节点和任何子节点。例如,你可以暂停整个场景:

self.pause = true 

甚至SKScene是一个“节点”。

+0

我想要做的就是停止计时器。当我点击球时计时器开始。所以我想要的是当球击中设备的底部框架时停止定时器。 –

+0

你的代码确实有帮助!我只需要改变它为我有一个!谢谢 !! –

+0

在我接受你给我的答案之前。有没有什么办法可以冻结计时器而不是将其删除? –