ios - 一旦计时器归零,如何使丢失标 checkout 现?

标签 ios swift timer sprite-kit

我是编码初学者,我需要以下代码的帮助。我正在努力制作“你输了!”标签在计时器为零后出现,但是当我尝试这样做时;它没有显示任何内容。请帮助我!

class Game: SKScene {

    let Ball = SKSpriteNode(imageNamed: "Red.png")
    var QuitOption = SKLabelNode()
    var ScoreLabel = SKLabelNode()
    var timesecond = Int(60)
    var locked = false
    var loseOption = SKLabelNode()
    var winOption = SKLabelNode()
    let whiteBall = SKSpriteNode(imageNamed: "whiteDot")
    let showMessage = SKLabelNode()

    override func didMoveToView(view: SKView) {


        backgroundColor = SKColor.whiteColor() // 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: 82, height: 82)
        Ball.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.1)
        Ball.physicsBody = SKPhysicsBody(circleOfRadius: 42)
        Ball.physicsBody?.affectedByGravity = true
        Ball.physicsBody?.density = 10
        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.redColor()
        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.fontColor = SKColor.redColor()
        ScoreLabel.fontSize = 35                 // 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 + 160, y: self.frame.size.height/1 - 115) // position of ScoreLabelNode
        ScoreLabel.name = "Score+"
        ScoreLabel.hidden = false

        self.addChild(ScoreLabel)

        whiteBall.size = CGSize(width: 55, height: 55)
        whiteBall.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.8)
        whiteBall.name = "whiteBall"

        self.addChild(whiteBall)

        showMessage.fontName = "Noteworthy-Light"
        showMessage.fontSize = 55
        showMessage.fontColor = SKColor.whiteColor()
        showMessage.text = "YOU LOSE!"
        showMessage.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7)
        showMessage.zPosition = 100
        showMessage.hidden = true
        showMessage.name = "show Message"


        self.addChild(showMessage)


    }

    // 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(self)

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

            }


        }



        if(!self.locked){



            self.locked = true

        var actionrun = SKAction.waitForDuration(0.5)

        var actionwait = SKAction.runBlock({


            self.timesecond--


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

            self.ScoreLabel.text = "\(self.timesecond)"})

            let loopAction = SKAction.repeatAction(SKAction.sequence([actionwait, actionrun]), count: 60)

            ScoreLabel.runAction(loopAction, withKey: "scoreAction")

            if (timesecond == 0){

                showMessage.hidden = false

            }


        }


    }


}

最佳答案

您的代码:

    if (timesecond == 0){

        showMessage.hidden = false

    }

...应该位于每秒运行的 block 中:

var actionwait = SKAction.runBlock({


    self.timesecond--

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

    self.ScoreLabel.text = "\(self.timesecond)"

    if (self.timesecond == 0){

        showMessage.hidden = false

    }
})

let loopAction = SKAction.repeatAction(SKAction.sequence([actionwait, actionrun]), count: 60)

ScoreLabel.runAction(loopAction, withKey: "scoreAction")

关于ios - 一旦计时器归零,如何使丢失标 checkout 现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30668905/

相关文章:

ios - IOS中如何在不下载页面内容的情况下获取服务器的状态

swift - 一旦事件日期过去,获取在 TableViewController 之间传递的信息 - Swift

ios - 自定义类中未调用委托(delegate)方法

Android ViewPager 自动换页

c# - 为什么 Timer_Tick 事件中的 SendKeys.SendWait 和 Thread.Sleep 没有阻塞?

ios - ssl中Client Hello包中mshkg000019.blob.core.windows.net的含义

ios - 对 touchBegan 示例感到困惑

ios - 我的核心数据模型出了什么问题?

Swift - 从服务器上的特定文件夹/远程 URL 中获取所有文件

c++ - 多线程崩溃