import UIKit import AVFoundation class ViewController: UIViewController { var bombSoundEffect: AVAudioPlayer? var timer = Timer() // defines a variable called screenSize var screenSize:CGRect = UIScreen.main.bounds // defines variables for the screen height and screen width var screenHeight = 0 var screenWidth = 0 // FINISH ME @IBOutlet weak var ship: UIImageView! // FINISH ME @IBOutlet weak var alien: UIImageView! // generic func moves the object that you send to the function func moveObjectDown(_ object:UIImageView) { // FINISH ME let x = object.frame.origin.? var y = object.frame.origin.? let width = object.frame.size.? let height = object.frame.size.? if Int(y) < screenHeight { // FINISH ME // increase y by 4 } else { y = -height } object.frame = CGRect(x: x, y: y, width: width, height: height) } @IBAction func moveUp(_ sender: UIButton) { let x = ship.frame.origin.x // FINISH ME var y = ship.frame.origin.? let width = ship.frame.size.width let height = ship.frame.size.height if y > 24 { // FINISH ME // decrease y by 4 } else { playSound() } ship.frame = CGRect(x: x, y: y, width: width, height: height) } @IBAction func moveDown(_ sender: UIButton) { // FINISH ME let x = ship.frame.origin.? var y = ship.frame.origin.? let width = ship.frame.size.? let height = ship.frame.size.? if y + height + 4 < CGFloat(screenHeight) { // FINISH ME // increase y by 4 } else { playSound() } ship.frame = CGRect(x: x, y: y, width: width, height: height) } // FINISH ME // handle moving right and left @IBAction func moveRight(_ sender: UIButton) { // FINISH ME var x = ship.frame.origin.? let y = ship.frame.origin.? let width = ship.frame.size.? let height = ship.frame.size.? if x + width + 4 < CGFloat(screenWidth) { // FINISH ME // increase x by 4 } else { playSound() } ship.frame = CGRect(x: x, y: y, width: width, height: height) // debug info print("x=\(x)") print("alien width=\(width)") print("screenWidth=\(screenWidth)") } @IBAction func moveLeft(_ sender: UIButton) { // FINISH ME } // plays a sound if you call it // the sound file must be dragged into the files section func playSound() { // Replace "explosion.mp3" with your sound file // Your sound file cannot be more than 2400 kb/second 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 :( } } // 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() } // this function gets called over and over again by the timer @objc func timerAction() { moveObjectDown(alien) } 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. } }