Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:重新启动游戏后多次调用的方法_Ios_Swift_Ios8 - Fatal编程技术网

Ios Swift:重新启动游戏后多次调用的方法

Ios Swift:重新启动游戏后多次调用的方法,ios,swift,ios8,Ios,Swift,Ios8,我正在做一个小游戏,用户必须点击一个圆圈10次。每次标签显示要点击的次数。因此,每次点击后,标签中的数字都会减少1。完成后,您将进入一个新视图,您可以点击“再次播放”按钮 问题是,当第二次播放时,每次点击的数字会减少2,第三次播放时会减少3,以此类推 代码如下: import UIKit var circleTapTrue = UIButton() var circleTapFalse1 = UIButton() var circleTapFalse2 = UIButton() var cir

我正在做一个小游戏,用户必须点击一个圆圈10次。每次标签显示要点击的次数。因此,每次点击后,标签中的数字都会减少1。完成后,您将进入一个新视图,您可以点击“再次播放”按钮

问题是,当第二次播放时,每次点击的数字会减少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!) {
}

}
也许我必须在游戏结束后移除物品? 我该怎么做呢


提前感谢

您的按钮存储在全局变量中。(即,在任何类的实例之外)这意味着它们将在视图控制器之间共享。每次调用viewDidLoad时,都会将另一个视图控制器的回调添加到相同的按钮


您应该将按钮存储为视图控制器的实例变量;只需将所有
var
声明移动到
class ViewController
花括号内。

当您按下重播按钮时,是否调用了游戏视图的
-viewDidLoad
方法?@68cherries是的,每次都会调用它非常感谢!这真的帮了大忙。