我正在尝试在具有与不同事物相关联的不同分值的游戏中保持分数。用户必须触摸游戏中的东西才能获得积分。然而,分数没有被准确计算。
我已经确定与游戏中每个元素相关联的分数正确存储在一个名为ballValue
. 该变量currentScore
用于保存分数,然后我使用 aSKLabelNode
来显示currentScore
.
currentScore
初始化为 0 值,并ballValue
在用户触摸球时使用其当前值进行更新。
更新以显示代码和上下文
class ChallengeScene: SKScene, GKGameCenterControllerDelegate {
// MARK: Ball Data
var randomBallType : Int = 0
var ballValue : Int = 0
// MARK: HUD
var scoreLabel = SKLabelNode(fontNamed: "Impact")
var timeLabel = SKLabelNode(fontNamed: "Impact")
var currentScore : Int = 0 {
didSet {
self.scoreLabel.text = "\(self.currentScore)"
GameHandler.sharedInstance.score = currentScore
}
}
// MARK: Initial Setup
func createHUD() {
... UNRELATED HUD STUFF...
currentScore = 0
}
// MARK: Gameplay Setup
func createBall(forTrack track: Int) {
setupTracks()
randomBallType = GKRandomSource.sharedRandom().nextInt(upperBound: 9)
switch randomBallType {
case 0:
player = SKSpriteNode(imageNamed: "blueOne")
ballSpeed = 0.008
ballValue = 1
case 1:
player = SKSpriteNode(imageNamed: "minusThree")
ballSpeed = 0.008
ballValue = -3
case 2:
player = SKSpriteNode(imageNamed: "minusFive")
ballSpeed = 0.008
ballValue = -5
case 3:
player = SKSpriteNode(imageNamed: "purpleThree")
ballSpeed = 0.008
ballValue = 3
case 4:
player = SKSpriteNode(imageNamed: "greenFive")
ballSpeed = 0.008
ballValue = 5
case 5:
player = SKSpriteNode(imageNamed: "blackTen")
ballSpeed = 0.008
ballValue = 10
default:
player = SKSpriteNode(imageNamed: "blueOne")
ballSpeed = 0.008
ballValue = 1
}
player?.name = "BALL"
player?.size = CGSize(width: 100, height: 100)
player?.physicsBody?.linearDamping = 0
...UNRELATED BALL STUFF...
self.addChild(player!)
}
// MARK: Overrides
override func didMove(to view: SKView) {
createHUD()
launchGameTimer()
self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.createBall(forTrack: self.track)
}, SKAction.wait(forDuration: 1)])))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node = self.nodes(at: location).first
if node?.name == "BALL" {
currentScore = currentScore + ballValue
node?.removeFromParent()
}
}
}
override func update(_ currentTime: TimeInterval) {
// GAME TIMER RELATED STUFF
}
}
我想我知道问题发生在哪里(在触摸覆盖中),我只是不明白为什么会发生。如果你能提供帮助,那就太棒了
您只有一个ballValue
属性。每次生成一个新球时,您都会ballValue
为新创建的球设置适当的值。其他所有现有的球呢?触摸它们中的任何一个也同样值得ballValue
。
当触球时,您需要知道触球的正确值,该值可能与最近创建的球的值不同。您可能想要做的是创建一个SKSpriteNode
代表球的子类,并拥有自己的价值:
class BallNode: SKSpriteNode {
let value: Int
init(imageName: String, speed: CGFloat, value: Int) {
let texture = SKTexture(imageNamed: imageName)
self.value = value
super.init(texture: texture, color: .init(white: 1, alpha: 0), size: texture.size())
self.speed = speed
}
required init(coder decoder: NSCoder) { fatalError() }
}
像这样创建球:
func createBall(forTrack track: Int) {
// 设置跟踪()
randomBallType = GKRandomSource.sharedRandom().nextInt(upperBound: 9)
let ball: BallNode
switch randomBallType {
case 0: ball = BallNode(imageNamed: "blueOne", speed: 0.008, value: 1)
case 1: ball = BallNode(imageNamed: "minusThree", speed: 0.008, value: -3)
case 2: ball = BallNode(imageNamed: "minusFive", speed: 0.008, value: -5)
// etc.
}
ball.name = "BALL"
ball.size = CGSize(width: 100, height: 100)
ball.physicsBody?.linearDamping = 0
// ...UNRELATED BALL STUFF...
self.addChild(ball)
}
然后,为了处理触球,使用触球的值来更新分数:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if let ball = self.nodes(at: location).flatMap({ $0 as? BallNode }).first {
currentScore += ball.value
ball.removeFromParent()
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句