Swift 雪碧套件变量赢得';不要停止添加
在我的游戏中,每当我的更新函数中出现某些条件时,我都试图在一个可变分数上加1。问题是更新函数在每一帧都被调用,所以我的score变量在缓慢上升1的时候会一直上升到数百。这是我的密码:Swift 雪碧套件变量赢得';不要停止添加,swift,sprite-kit,Swift,Sprite Kit,在我的游戏中,每当我的更新函数中出现某些条件时,我都试图在一个可变分数上加1。问题是更新函数在每一帧都被调用,所以我的score变量在缓慢上升1的时候会一直上升到数百。这是我的密码: override func update(_ currentTime: TimeInterval) { if circleStart.frame.width >= self.frame.width && userTouched == false{ hasTouched
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)
}
覆盖函数更新(\uCurrentTime:TimeInterval){
如果circleStart.frame.width>=self.frame.width&&userTouched==false{
hastTouchedge=true
}
如果hastTouchedge==true&&userTouched==false{
缩放速度-=0.2
}
如果scaleSpeed acceptableBound2&&circleLengthacceptableBound1{
gameOverLabel.ishiden=false
playreach.ishiden=false
再次播放。运行(fadingTapIn)
}
如果userTouched==true&&circleLength
我需要做什么或添加什么才能使我的分数变量以我想要的方式在上面解释?简易解决方案:
在得分+=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)
}
}
}
您可以使用ToucheSBegind、touchesMoved和touchesEnded作为函数
查看Ray Wenderlich的SpriteKit教程,您将学到很多 您的示例中缺少一些代码。用户触摸设置在哪里?如果分数每一帧都在增加,那么
usertouch
每一帧都是真的。猜测一下,这也意味着,circleLength
、acceptableBound1
和acceptableBound2
从未改变。因此,如果导致打印“工作”的测试一次为真,那么每次都是真的。
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
}