简体   繁体   English

如何停止计时器?

[英]How can I stop the timer?

I just started to learn how to program games for Apple and i've been searching how to stop a timer. 我刚开始学习如何为Apple编写游戏程序,并且一直在寻找如何停止计时器的方法。 In the following code i have i want to stop the timer i made but I'm having trouble how to stop it. 在下面的代码中,我想停止计时器,但是我很难停止它。 i have no clue. 我没有任何线索。 Please help me! 请帮我! Thank you! 谢谢!

class Game: SKScene { 类游戏: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])))



    }


}

} }

Not a 100% sure what if I understand your question. 不能100%确定我是否理解您的问题。 But if "timer" is referring to your looping action, you could do this: 但是,如果“定时器”是指您的循环操作,则可以执行以下操作:

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

Later somewhere in you code you can stop this action by: 稍后在代码中的某个位置,您可以通过以下方式停止此操作:

ScoreLabel.removeActionForKey("scoreAction")

UPDATE: 更新:

If you only want to pause/un-pause animations you can use the paused property on the objects you want to pause. 如果只想暂停/取消暂停动画,则可以对要暂停的对象使用paused属性。

ScoreLabel.pause = true

It is good to know that the .pause will affect the Node and any child nodes. 很高兴知道.pause会影响Node和任何子节点。 You could pause the whole scene for instance: 例如,您可以暂停整个场景:

self.pause = true

Even the SKScene is a "node". 甚至SKScene都是一个“节点”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM