【问题标题】:Score based on time基于时间的分数
【发布时间】:2016-03-23 23:23:56
【问题描述】:

我对 Swift 完全陌生(昨天开始研究这个)所以我不知道从哪里开始。

基本上我正在制作一个分数每 0.25 秒递增一次的游戏(游戏开始后 - touchesBegan)。我在想waitForDuration(0.25),但我不知道该怎么办。

而且我还必须将它放置在屏幕的顶部中心(横向)。我尝试使用.position,但没有成功。

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    我做到了!!我的意思是,我找到了一些有助于做到这一点的代码、视频和文本 =)

    //Score
    scoreLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
    self.addChild(scoreLabel)
    
    //Score incrementing by time
    let delay = SKAction.waitForDuration(0.25)
    let incrementScore = SKAction.runBlock ({
        self.score = self.score + 1
        self.scoreLabel.text = "\(self.score)"
    })
    self.runAction(SKAction.repeatActionForever(SKAction.sequence([delay,incrementScore])))
    

    我没有使用 NSTimer,因为我读到它不能暂停/恢复(我需要稍后做)。

    谢谢你=)

    【讨论】:

      【解决方案2】:

      欢迎来到 SO。你的问题真的很模糊而且很广泛。不适合这个网站。

      既然你是新手,我会给你一个提示让你开始。在寻求更多帮助之前,请先阅读并尝试自行完成。

      使用创建一个重复的 NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

      使计时器调用的方法增加一个分数实例变量并更新一个标签

      【讨论】:

      • 不要在 Sprite-Kit 中使用 NSTimer - 场景不会知道定时器,它会搞砸(例如,如果你暂停 SKScene,定时器也不会暂停)。通过分析自上次调用 Update() 以来经过的时间,使用 SKActions 或在 Update() 中执行操作。
      • 很公平。我没用过 SpriteKit。
      猜你喜欢
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2016-05-28
      • 2019-04-21
      • 1970-01-01
      • 2020-08-11
      相关资源
      最近更新 更多