gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:50:52 24 4
gpt4 key购买 nike

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

当我明确更改代码以添加整数(例如 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com