我制作了一个小游戏,用户必须点击一个圆圈10次。每次标签显示要点击的次数。因此,每次点击后,标签中的数字减少一个。完成后,您将进入一个新的视图,在那里您可以点击“再次播放”#34;按钮。
问题在于,当第二次播放时,每次敲击时数字减少2,第三次播放时减少3,依此类推。
以下是代码:
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!) {
}
}
也许我必须在完成游戏后删除对象? 我应该怎么做?
提前致谢
答案 0 :(得分:2)
您的按钮存储在全局变量中。 (即在任何类的实例之外)这意味着它们将在您的视图控制器之间共享。每次调用viewDidLoad时,您都会将另一个视图控制器的回调添加到相同的按钮。
您应该将按钮存储为视图控制器的实例变量;只需在var
花括号中移动所有class ViewController
声明。