ios - Swift: method called more than once after restarting game -


i'm making little game user has tap on circle 10 times. each time label shows how many times tap left. after every tap number in label decreases one. after finishing brought new view can tap "play again" button.

the problem when playing second time number decreases 2 on every tap , when playing third time 3 , on.

here's code:

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()     // additional setup after loading view, typically 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 resources can 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!) { }  } 

maybe have remove objects after finishing game? , how should it?

thanks in advance

you have buttons stored in global variables. (i.e. outside of class's instance) means they'll shared across view controllers. each time viewdidload called, you're adding view controller's callbacks same buttons.

you should store buttons instance variables of view controller instead; move var declarations inside class viewcontroller curly braces.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -