![](/img/trans.png)
[英]Game Crashes after applicationWillResignActive is called more than once
[英]Swift: method called more than once after restarting game
我正在做一个小游戏,用户必须在一个圆圈上轻按10次。 每次标签显示剩余点击次数。 因此,每次轻按后,标签中的数字将减少1。 完成后,您将进入一个新视图,您可以在其中点击“再次播放”按钮。
问题在于,第二次播放时,每次敲击时该数字减少2,而第三次播放时则减少三以此类推。
这是代码:
import UIKit
var circleTapTrue = UIButton()
var circleTapFalse1 = UIButton()
var circleTapFalse2 = UIButton()
var circleTapFalse3 = UIButton()
var circleTapFalse4 = UIButton()
var lblTappingSpeed = UILabel()
var tappingSpeed = 0.00
var lblMovesLeft = UILabel()
var movesLeft = Int()
var imgCircleWidthHeight = Float()
var imgCircleXPos = Float()
var imgCircleYPos = Float()
class ViewController: UIViewController {
var timerCountDown = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
moveCircles()
movesLeft = 10
lblMovesLeft.text = "\(movesLeft)"
var circleImageNames = ["CircleTap", "CircleLightGray", "CirclePink", "CircleViolet", "CircleYellow"]
circleTapFalse1.setImage(UIImage(named: circleImageNames[1]), forState: UIControlState.Normal)
circleTapFalse1.setImage(UIImage(named: circleImageNames[1]), forState: UIControlState.Highlighted)
circleTapFalse1.addTarget(self, action: "falseCircleTouched", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(circleTapFalse1)
circleTapFalse2.setImage(UIImage(named: circleImageNames[2]), forState: UIControlState.Normal)
circleTapFalse2.setImage(UIImage(named: circleImageNames[2]), forState: UIControlState.Highlighted)
circleTapFalse2.addTarget(self, action: "falseCircleTouched", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(circleTapFalse2)
circleTapFalse3.setImage(UIImage(named: circleImageNames[3]), forState: UIControlState.Normal)
circleTapFalse3.setImage(UIImage(named: circleImageNames[3]), forState: UIControlState.Highlighted)
circleTapFalse3.addTarget(self, action: "falseCircleTouched", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(circleTapFalse3)
circleTapFalse4.setImage(UIImage(named: circleImageNames[4]), forState: UIControlState.Normal)
circleTapFalse4.setImage(UIImage(named: circleImageNames[4]), forState: UIControlState.Highlighted)
circleTapFalse4.addTarget(self, action: "falseCircleTouched", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(circleTapFalse4)
circleTapTrue.setImage(UIImage(named: circleImageNames[0]), forState: UIControlState.Normal)
circleTapTrue.setImage(UIImage(named: circleImageNames[0]), forState: UIControlState.Highlighted)
circleTapTrue.addTarget(self, action: "moveCircles", forControlEvents: UIControlEvents.TouchDown)
self.view.addSubview(circleTapTrue)
lblTappingSpeed.frame = CGRectMake(200, 20, 100, 21)
lblTappingSpeed.text = "\(tappingSpeed)"
lblTappingSpeed.textAlignment = NSTextAlignment.Right
self.view.addSubview(lblTappingSpeed)
lblMovesLeft.frame = CGRectMake(20, 20, 100, 21)
self.view.addSubview(lblMovesLeft)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
timerCountDown = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "TapSpeed", userInfo: nil, repeats: true)
}
func moveCircles() {
getRandomCirclePositionAndSize()
UIView.beginAnimations("moveCircle", context: nil)
UIView.setAnimationDuration(0.2)
circleTapFalse1.frame = CGRectMake(imgCircleXPos, imgCircleYPos, imgCircleWidthHeight, imgCircleWidthHeight)
UIView.commitAnimations()
getRandomCirclePositionAndSize()
UIView.beginAnimations("moveCircle", context: nil)
UIView.setAnimationDuration(0.2)
circleTapFalse2.frame = CGRectMake(imgCircleXPos, imgCircleYPos, imgCircleWidthHeight, imgCircleWidthHeight)
UIView.commitAnimations()
getRandomCirclePositionAndSize()
UIView.beginAnimations("moveCircle", context: nil)
UIView.setAnimationDuration(0.2)
circleTapFalse3.frame = CGRectMake(imgCircleXPos, imgCircleYPos, imgCircleWidthHeight, imgCircleWidthHeight)
UIView.commitAnimations()
getRandomCirclePositionAndSize()
UIView.beginAnimations("moveCircle", context: nil)
UIView.setAnimationDuration(0.2)
circleTapFalse4.frame = CGRectMake(imgCircleXPos, imgCircleYPos, imgCircleWidthHeight, imgCircleWidthHeight)
UIView.commitAnimations()
getRandomCirclePositionAndSize()
UIView.beginAnimations("moveCircle", context: nil)
UIView.setAnimationDuration(0.2)
circleTapTrue.frame = CGRectMake(imgCircleXPos, imgCircleYPos, imgCircleWidthHeight, imgCircleWidthHeight)
UIView.commitAnimations()
movesLeft--
lblMovesLeft.text = "\(movesLeft)"
if movesLeft == 0 {
gameFinished()
}
}
func getRandomCirclePositionAndSize() {
imgCircleWidthHeight = (Float(arc4random()) % 80) + 20
imgCircleXPos = Float(arc4random()) % (320 - imgCircleWidthHeight)
imgCircleYPos = (Float(arc4random()) % (460 - imgCircleWidthHeight)) + 20
}
func falseCircleTouched() {
movesLeft++
lblMovesLeft.text = "\(movesLeft)"
}
func TapSpeed() {
tappingSpeed = tappingSpeed + 0.01
lblTappingSpeed.text = NSString(format: "%.2f", tappingSpeed)
if movesLeft == 0 {
timerCountDown.invalidate()
}
}
func gameFinished() {
tappingSpeed = 0
lblTappingSpeed.text = NSString(format: "%.2f", tappingSpeed)
timerCountDown.invalidate()
self.performSegueWithIdentifier("test", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
}
}
也许我必须在游戏结束后移除物体? 我该怎么办呢?
提前致谢
您已将按钮存储在全局变量中。 (即在任何类的实例外部),这意味着它们将在您的视图控制器之间共享。 每次调用viewDidLoad时,您都将另一个视图控制器的回调添加到相同的按钮。
您应该将按钮存储为视图控制器的实例变量。 只需将所有var
声明移到class ViewController
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.