在我的游戏中,每次在我的更新功能中发生某些条件时,我都会尝试将1添加到变量得分中。问题是每帧都会调用更新函数,所以当我的分数变量缓慢上升1时,我的分数变量就会不断攀升到数百。这是我的代码:
override func update(_ currentTime: TimeInterval) {
if circleStart.frame.width >= self.frame.width && userTouched == false{
hasTouchedEdge = true
}
if hasTouchedEdge == true && userTouched == false {
scaleSpeed -= 0.2
}
if scaleSpeed <= 1.0 && userTouched == false {
scaleSpeed += 0.3
hasTouchedEdge = false
}
if userTouched == false{
scaleSpeed += 0.1
circleStart.setScale(CGFloat(scaleSpeed))
circleLength = circleStart.frame.width
acceptableBound1 = outerCircle.frame.width
acceptableBound2 = outerCircle.frame.width - 100
}
if userTouched == true && circleLength > acceptableBound2 && circleLength < acceptableBound1{
print("worked")
isCorrect = true
}
if userTouched == true && circleLength > acceptableBound1 {
gameOverLabel.isHidden = false
playAgain.isHidden = false
playAgain.run(fadingTapIn)
}
if userTouched == true && circleLength < acceptableBound2{
gameOverLabel.isHidden = false
playAgain.isHidden = false
playAgain.run(fadingTapIn)
}
if isCorrect == true {
score += 1
}
scoreLabel.text = String(score)
}
我需要做什么或添加什么才能让我的得分变量按上述方式工作?
答案 0 :(得分:0)
简易解决方案:
在isCorrect = false
之后添加score += 1
。
好的解决方案: 尝试删除更新中的大部分代码。在大多数情况下,更新被称为每秒近60次,并且由于某些原因,它不是进行更改的最佳位置。
尝试使用
override func touchesBegan(touches: Set<UITouch>,with event: UIEvent?) {
for touch in touches {
let location = touch.location
if (someNode.contains(location)) {
//This if statement isn't needed, but an example of how you can determine if a node was pressed
someFunctionToDoStuff(location)
}
}
}
或
override func touchesBegan(touches: Set<UITouch>,with event: UIEvent?) {
let touch = touches.first
let location = touch.location
if (someNode.contains(location)) {
//This if statement isn't needed, but an example of how you can determine if a node was pressed
someFunctionToDoStuff(location)
}
}
第一个选项可能会多次运行,具体取决于确定的触摸次数。第二种选择只会注意它注意到的第一次触摸并忽略其余部分。
您可以根据需要定义此功能,只需确保在需要时获取触摸位置:
func someFunctionToDoStuff(_ location: CGPoint) {
score += 1
// Then maybe move a node to where the press was located
ball.position = location
}
您可以将touchesBegan,touchesMoved和touchesEnded用作函数。
查看Ray Wenderlich的SpriteKit教程,你将学到很多东西! https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners