ios - Sprite Kit 中的增量分数显示在 HUD 中

标签 ios swift sprite-kit

我知道我已经很接近在游戏中提高分数了。

当我明确更改代码以添加整数(例如 5)而不是“%d”时,触摸硬币时分数会显示在 HUD 中:

func didBeginContact(contact: SKPhysicsContact) {
    lblScore.text = String(format: "%d", GameState.sharedInstance.score)
} 

至:

func didBeginContact(contact: SKPhysicsContact) {
    lblScore.text = String(format: "5", GameState.sharedInstance.score)
}

enter image description here

但是,如果我保留“%d”,则什么也不会发生。我不确定如何增加 HUD 中的分数或在哪里进行更改。

这是其余的代码。

GameScene.swift:

struct PhysicsCategory {

  static let None: UInt32              = 0
  static let Player: UInt32            = 0b1     
  static let CoinNormal: UInt32        = 0b1000  
  static let CoinSpecial: UInt32       = 0b10000  
}

class GameScene: SKScene, SKPhysicsContactDelegate {

 // HUD 
 var hudNode: SKNode!
 var lblScore: SKLabelNode!
 var lblCoins: SKLabelNode!

 override func didMoveToView(view: SKView) {

   // HUD
   hudNode = SKNode()
   hudNode.zPosition = 1000
   cameraNode.addChild(hudNode)


   // Coins
   // 1
   let coin = SKSpriteNode(imageNamed: "powerup05_1")
   coin.position = convertPoint(CGPoint(x: 300, y: self.size.height-100), toNode: cameraNode)
   coin.zPosition = 1000
   hudNode.addChild(coin)

   // 2
   lblCoins = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
   lblCoins.fontSize = 70
   lblCoins.fontColor = SKColor.whiteColor()
   lblCoins.position = convertPoint(CGPoint(x: 375, y: self.size.height-100), toNode: cameraNode)
   lblCoins.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
   lblCoins.zPosition = 1000

   // 3
   lblCoins.text = String(format: "X %d", GameState.sharedInstance.coins)
   hudNode.addChild(lblCoins)

   // Score
   // 4
   lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
   lblScore.fontSize = 70
   lblScore.fontColor = SKColor.whiteColor()
   lblScore.position = convertPoint(CGPoint(x: self.size.width-325, y: self.size.height-100), toNode: cameraNode)
   lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
   lblScore.zPosition = 1000

   // 5
   lblScore.text = "0"
   hudNode.addChild(lblScore)
  }
}

func didBeginContact(contact: SKPhysicsContact) {

  lblScore.text = String(format: "%d", GameState.sharedInstance.score)
}

GameState.swift:

class GameState {
  var score: Int
  var highScore: Int
  var coins: Int

  init() {
    // Init
    score = 0
    highScore = 0
    coins = 0

    // Load game state
    let defaults = NSUserDefaults.standardUserDefaults()

    highScore = defaults.integerForKey("highScore")
    coins = defaults.integerForKey("coins")
  }

  func saveState() {
    // Update highScore if the current score is greater
    highScore = max(score, highScore)

    score = max(score, highScore)

    // Store in user defaults
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setInteger(highScore, forKey: "highScore")
    defaults.setInteger(coins, forKey: "coins")
    NSUserDefaults.standardUserDefaults().synchronize()
  }

  class var sharedInstance: GameState {
    struct Singleton {
        static let instance = GameState()
    }

    return Singleton.instance
  }
}

最佳答案

正如我所见,您正在使用 GameState.sharedInstance.score 来存储分数。但你从来不更新它。每次玩家得分时,您都必须增加分数。这部分:

 lblScore.text = String(format: "%d", GameState.sharedInstance.score)

仅读取分数变量。

这部分也没有意义:

highScore = max(score, highScore)

score = max(score, highScore)  //remove this

score 变量与 highscore 变量不同。

关于ios - Sprite Kit 中的增量分数显示在 HUD 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34300693/

相关文章:

iphone - 使用 Sprite Kit 粒子的 iOS 烟花爆炸

objective-c - iOS:如何使用按钮来切换 Tableview 行?

ios - 如何以编程方式为ios中的 Collection View 单元格提供宽度和高度,swift 3

ios - 如何在保持宽高比和大小的同时合并两个 UIImages?

ios - Firebase 使用电子邮件、密码、显示名称和照片网址创建用户

ios - 应用忽略 supportedInterfaceOrientations

ios - 随时随地在 UILabel.attributedText 上应用不同的属性

ios - 如何缓存或预加载 SKLabelNode 字体?

ios - 用于渲染自定义 map (Spritekit 或原始 Metal )的有效 MacOS/iOS 框架?

ios - 如何在应用程序中强制 UIApplication DidEnterBackgroundNotification