//NS Timer For Tubes and Birds

   
//IMPORTANT**** move screenWidth and height to class variables //1) place this code under class declaration (class variables) var tubeTimer = NSTimer() var birdTimer = NSTimer() // var tTubeX=CGFloat()//this was added to keep track of top tubes x coordinates var goUp=false var counter=0 //2) call this method in start Game IBAction func moveThings(){ tubeTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("moveTubes"), userInfo: nil, repeats: true) birdTimer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("moveBird"), userInfo: nil, repeats: true) } func moveTubes(){ //will fill this in with code to move the tubes tTubeX=tTubeX-5 topTube.frame.origin = CGPoint(x: tTubeX, y: 0) self.view.addSubview(topTube)//you may need to add outlet to view again. //check to bird crashing into tube if(CGRectIntersectsRect(bird.frame, topTube.frame)){ println("crash") } } func moveBird(){ //will fill this in with code to move the bird //will fill this in with code to move the bird if (goUp==false){ birdY=birdY+5; }else{ counter++ if(counter>6){ goUp=false counter=0 }else{ birdY=birdY-5; } } self.bird.center=CGPointMake(50,birdY); } //4) Add the touches began method override func touchesBegan(touches: Set, withEvent event: UIEvent) { println("touch") birdY=birdY-10 goUp=true } //5)call this when you want to stop the Timer - when bird hits tube tubeTimer.invalidate() birdTimer.invalidate() //another method you could use to position elements on screen is center //self.tube1.center=CGPointMake(45,54);//no need to add back to screen again