在我的游戏中,每当更新函数中出现某些条件时,我都会尝试将变量分数加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)
}我需要做什么或添加什么才能让我的分数变量以我想要的方式在上面解释?
发布于 2017-04-21 04:08:34
简单的解决方案:在score += 1之后添加isCorrect = false。
好的解决方案:尝试删除更新中的大部分代码。在大多数情况下,更新几乎每秒调用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
https://stackoverflow.com/questions/43509630
复制相似问题