import UIKit import AVFoundation ball1 class ViewController: UIViewController { var dirOfBall1 = "DOWN" // FINISH ME // add a variable called dirOfBall2 and // assign it "RIGHT" ??? ????? = ???? // defines a variable called screenSize var screenSize:CGRect = UIScreen.main.bounds // defines variables for the screen height and screen width // these will get their values in the viewDidLoad() function (method) var screenHeight = 0 var screenWidth = 0 // this variable refers to the timer object var timer = Timer() // this variable refers to the audio player var bombSoundEffect: AVAudioPlayer? // FINISH ME // Control + Drag your ball1 to here @IBOutlet weak var ball1: UIImageView! // FINISH ME // Control + Drag your ball2 to here @IBOutlet weak var ball2: UIImageView! // FINISH ME // Control + Drag your start button here @IBAction func startButtonEvent(_ sender: UIButton) { // shut down the timer timer.invalidate() // start the timer // and call timerAction() every 0.1 seconds timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true) } // FINISH ME // Control + Drag your stop button here @IBAction func stopButtonEvent(_ sender: UIButton) { // shut down the timer timer.invalidate() } @objc func timerAction() { // we will first get the height and // width of the ball1 // FINISH ME let height = ball1.frame.size.???? let width = ball1.frame.size.???? // FINISH ME // move the 2 objects // back and forth // 1 vertically back and forth // 1 horizontally back and forth // I have done most of the first ball for you // You will need to handle the second ball // see if the dirOfBall1 is equal to "UP" if ???????? == ???? { // FINISH ME let x = ball1.frame.origin.? var y = ball1.frame.origin.? // see if y is greater than 4 if ? ? ? { // FINISH ME // decrease y by 4 ?????? } else { // FINISH ME // increase y by 4 ???? dirOfBall1 = "DOWN" } ball1.frame = CGRect(x: x, y: y, width: width, height: height) } else { // FINISH ME let x = ball1.frame.origin.? var y = ball1.frame.origin.? if y + height + 4 < CGFloat(screenHeight) { // FINISH ME // increase y by 4 ????? } else { // decrease y by 4 ???? // FINISH ME // set dirOfBall1 to "UP" (assign it the value "UP") dirOfBall1 = "UP" } // now we reset the frame of the ball1 ball1.frame = CGRect(x: x, y: y, width: width, height: height) // debug info print("y=\(y)") print("ball1 height=\(height)") print("screenHeight=\(screenHeight)") } // FINISH ME // handle the second ball here // see if dirOfBall2 is equal to the String "LEFT" if ??? ?? ????? { // FINISH ME var x = ball2.frame.origin.? let y = ball2.frame.origin.? // FINISH ME // see if x is greater than 4 (DO NOT USE let) ?? ? > ? { // FINISH ME // decrease x by 4 ????? } else { // FINISH ME // increase x by 4 ???? // FINISH ME // set dirOfBall2 to "RIGHT" (assign it the value "RIGHT") ????? = ????? } ball2.frame = CGRect(x: x, y: y, width: width, height: height) } else { // FINISH ME var x = ball2.frame.origin.? let y = ball2.frame.origin.? if x + width + 4 < CGFloat(screenWidth) { // FINISH ME // increase x by 4 ???? } else { // decrease x by 4 ???? dirOfBall2 = "LEFT" } // now we reset the frame of the ball1 ball2.frame = CGRect(x: x, y: y, width: width, height: height) print("x=\(x)") print("width=\(width)") print("screenWidth=\(screenWidth)") } // play a sound if they collide if ball2.frame.intersects(ball1.frame) { // FINISH ME // call the function (method) playSound() // You call a method by it's name and parenthesis ????????? } } // DO NOT CHANGE ANYTHING BELOW EXCEPT FOR MAYBE // "explosion.mp3" if you have your own short sound clip func playSound() { let path = Bundle.main.path(forResource: "explosion.mp3", ofType:nil)! let url = URL(fileURLWithPath: path) do { bombSoundEffect = try AVAudioPlayer(contentsOf: url) bombSoundEffect?.play() } catch { // couldn't load file :( } } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // here we get the width and height of the screen screenSize = UIScreen.main.bounds screenWidth = Int(screenSize.width) screenHeight = Int(screenSize.height) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }