Radu Raducu
Radu Raducu

Reputation: 33

How to link an image to a sound randomly?

I have this code and it's working, but not as really want to. I'm new to Xcode/Swift/developing apps. I've made it watching tutorials and using some snippets as examples, but so far is working :)

  1. I'd like to make it work in this way:

    When I click on playSound and hear it, then I have to choose between leftImage and rightImage..the image that matches with the playSound. I've tried into many ways to get it work.. but nothing...I cannot make the sound index a string to compare as "if a == b .."

  2. When I open the app, it shows me only two bottom buttons.. but not any image. How can I make it to show the first image?

  3. I also like to make it a bit random think... When I click on "nextImage" button, I'd like to shows to different images but only one linked to the sound..so when I play the sound..had to check only the photo who matches with the sound.

  4. At this moment, I have only 8 photos/sounds into the array, but when I click more than 9 times on nextImage.. the images are going on and on.. but the sound starts from beginning and it's not linked any more. for example, at the 10th image..it playSound says it's at 1. How can I make It to follow the image index?

  5. How to convert the image index into text? for example, if its shows me the image "foto1"; I'd like to show me under the image a text in the label.

import UIKit
import AVFoundation

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

var soundFiles: [String] = [
    "s0",
    "s1",
    "s2",
    "s3",
    "s4",
    "s5",
    "s6",
    "s7",
    "s8",
    "s9"
]

var images1: [UIImage] = [
    
    UIImage(named: "foto1.png")!,
    UIImage(named: "foto2.png")!,
    UIImage(named: "foto3.png")!,
    UIImage(named: "foto4.png")!,
    UIImage(named: "foto5.png")!,
    UIImage(named: "foto6.png")!,
    UIImage(named: "foto7.png")!,
    UIImage(named: "foto8.png")!
]

var images2: [UIImage] = [
    
    UIImage(named: "foto1.png")!,
    UIImage(named: "foto2.png")!,
    UIImage(named: "foto3.png")!,
    UIImage(named: "foto4.png")!,
    UIImage(named: "foto5.png")!,
    UIImage(named: "foto6.png")!,
    UIImage(named: "foto7.png")!,
    UIImage(named: "foto8.png")!
]

var happySad: [UIImage] = [
    UIImage(named: "sad.png")!,
    UIImage(named: "happy.png")!
    ]

var currentImageIndex = 0
var currentImage2Index = 0


@IBOutlet weak var leftImage: UIImageView!

@IBOutlet weak var rightImage: UIImageView!

@IBOutlet weak var sh: UIImageView!





@IBAction func nextImages(_ sender: Any) {
 currentImageIndex += 1
    let numberOfImages = images1.count
    let nextImage1Index = currentImageIndex % numberOfImages
    leftImage.image = images1[nextImage1Index]
    leftImage.isUserInteractionEnabled = true
    self.view.addSubview(leftImage)
    let gesture1 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap1))
    leftImage.addGestureRecognizer(gesture1)
    
    
 currentImage2Index += 1
    let numberOfImages2 = images2.count
    let nextImage2Index = currentImage2Index % numberOfImages2
    rightImage.image = images2[nextImage2Index]
    sh.image = UIImage(named: "question")
    rightImage.isUserInteractionEnabled = true
    self.view.addSubview(rightImage)
    let gesture2 = UITapGestureRecognizer(target: self, action: #selector(ViewController.singleTap2))
    rightImage.addGestureRecognizer(gesture2)

}

func singleTap1() {
    if currentImageIndex == currentImage2Index {
        sh.image = UIImage(named: "happy.png")
        print("ok")
    } else {
        sh.image = UIImage(named: "sad.png")
        print("not ok")
    }
}
func singleTap2() {
    if currentImageIndex == currentImage2Index {
        sh.image = UIImage(named: "happy.png")
        print("ok2")
    } else {
        sh.image = UIImage(named: "sad.png")
        print("not ok2")
    }
}


var player: AVAudioPlayer!

@IBAction func playSound(_ sender: Any) {
    let numberOfImages = images1.count
    let nextImage5Index = currentImageIndex % numberOfImages
    let soundFilePath = Bundle.main.url(forResource: soundFiles[nextImage5Index], withExtension: ".m4a")!
    player = try! AVAudioPlayer(contentsOf: soundFilePath)
    player.prepareToPlay()
    player.play()

}
}

Upvotes: 1

Views: 116

Answers (1)

Scriptable
Scriptable

Reputation: 19750

I think you are over complicating the issue.

I would personally create a struct that contains each 'level' of the game.

enum correctImageType {
    case left, right
}

struct Level {
   var word: String
   var leftImage: UIImage
   var rightImage: UIImage
   var soundFile: String
   var correctImage: correctImageType
}

var level1 = Level(word: "Dog", leftImage: UIImage(named: "dog"), rightImage: UIImage(named: "cat", soundFile: "Woof", correctImage: .left)

This gives you enough information in one data structure to display each level. You can then create an array of these items, sort them, random sort them, mark as complete as each is done etc.

There are alot of questions within your question. I would try to make it more specific and tackle one particular part of the problem at a time. There is no problem in posting multiple questions as long as they are different.

Upvotes: 1

Related Questions