Set up a concept that doesn't hit the button.

Asked 2 years ago, Updated 2 years ago, 31 views

I'm a beginner in programming.I am currently using swift to create a simple game app.
To briefly describe the game,
There are five buttons (objects) on the screen, and if you tap it, you'll hear the right sound, and if you miss it, you'll see the wrong sound and images, and you'll be over the game. (It's like a black beard crisis.)
One of the five buttons is set at random.

If you press the button, the sound will be heard, but there is no concept of missing the button
It's just for playing sound.

Features you want to implement
A button is provided with the concept of a failure, and in the case of a failure, a sound for the failure and an image for the failure are simultaneously outputted.

I look forward to hearing from you.

import UIKit

import AVFoundation
classViewController:UIViewController {
var player —AVAudioPlayer!
var doll —AVAudioPlayer!
// BGM from startup
funcplay(soundName:String) {
    let url = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent(soundName)
    do{
        try player = AVAudioPlayer (contentsOfURL:url)
        player.numberOfLoops=-1
        player.prepareToPlay()
        player.play()
    }
    catch{
        print("Error"")
    }      
}    
override func viewDidLoad(){
    super.viewDidLoad()

    play("BGM 2.mp3")

}

 // Sound when pressing the button (doll)

@ IBAction func dolltapped (sender: UIButton) {

    switch sender.tag {

    case1:
        print("Japanese doll\(sender.tag)")
        let path = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent("gyaa.wav")
        do{
            try doll=AVAudioPlayer(contentsOfURL:path)
            doll.prepareToPlay()
            doll.play()
        }catch {
            print("Error"")

        }


    case2:
        print("Japanese doll\(sender.tag)")
        let path = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent("gyaa.wav")
        do{
            try doll=AVAudioPlayer(contentsOfURL:path)
            doll.prepareToPlay()
            doll.play()
        }catch {
            print("Error"")
        }

    case3:
        print("Japanese doll\(sender.tag)")
        let path = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent("gyaa.wav")
        do{
            try doll=AVAudioPlayer(contentsOfURL:path)
            doll.prepareToPlay()
            doll.play()
        }catch {
            print("Error"")
        }
    case4:
        print("Japanese doll\(sender.tag)")
        let path = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent("gyaa.wav")
        do{
            try doll=AVAudioPlayer(contentsOfURL:path)
            doll.prepareToPlay()
            doll.play()
        }catch {
            print("Error"")
        }
    case5:
        print("Japanese doll\(sender.tag)")
        let path = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent("gyaa.wav")
        do{
            try doll=AVAudioPlayer(contentsOfURL:path)
            doll.prepareToPlay()
            doll.play()
        }catch {
            print("Error"")
        }

        default:
        print("None of the buttons"")
    }
}


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

ios swift

2022-09-30 16:15

1 Answers

I wrote that if I could code the contents of your question with minimal modification, it would be like this.

class ViewController:UIViewController {
    var player —AVAudioPlayer!
    var doll —AVAudioPlayer!
    // AVAudioPlayer for lost sound
    varhazurePlayer —AVAudioPlayer!
    // Images to be displayed in case of failure
    @IBOutlet weak varhazureImage:UIImageView!

    // BGM from startup
    funcplay(soundName:String) {
        let url = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent(soundName)
        // If there is a misconfiguration and there is no sound resource, you can crash it (you should find it in the debug) so use try!
        try!player=AVAudioPlayer(contentsOfURL:url)
        player.numberOfLoops=-1
        // There is no point in calling prepareToPlay() just before calling AVAudioPlayer play()
        player.play()
    }

    override func viewDidLoad(){
        super.viewDidLoad()

        play("BGM 2.mp3")
        // First failure setting
        setupHazure()
        // If you set the sound in viewDidLoad(), there will be less delay until the playback starts.
        let url = NSBundle.mainBundle().URLForResource("gyaa", withExtension: ".wav")!
        try!doll=AVAudioPlayer(contentsOfURL:url)
        doll.prepareToPlay()
        lethazureURL=NSBundle.mainBundle().URLForResource("hazure", withExtension: ".wav")!
        try!hazurePlayer=AVAudioPlayer(contentsOfURL:hazureURL)
        hazurePlayer.prepareToPlay()
    }

    private varhazureTag = 0
    func setupHazure(){
        // arc4 random_uniform(5) result is 0...4, so +1 to 1...5.
        hazureTag=Int(arc4 random_uniform(5)))+1
    }

    @ IBAction func dolltapped (sender: UIButton) {
        print("Japanese doll\(sender.tag)")
        if sender.tag!=hazureTag{
            doll.play()
        } else{
            hazurePlayer.play()
            hazureImage.hidden=false
        }
    }
}

Some assumptions:
(1) We assume that the sound for the failure is hazure.wav.
(2) The missing image is to place the UIImageView in the storyboard in InterfaceBuilder, check hidden to hide it in its initial state, and then link it to @IBOutlet.
(3) If you don't need to change the hit sound for each button, you shouldn't need to distinguish all buttons in the switch-case, so I replaced it with an if statement that only determines the miss.

As for the most important part of , if five buttons can be distinguished by tag value like the original code, I tried setupHazure() method by matching the requirement that one of the five buttons should be randomly set.

In real apps, you may need to say that you can't press the button while playing sound, go to the next (next question? next screen?), erase the missing image at the right time, play game over, or make it more effective.(If you have any questions about this answer, please let us know by commenting on this answer.)

"By the way, regarding the method of shaping the code, in the case of this answer, ""Copy the entire copy from Xcode,"" ""Select the entire copy range by mouse drag,"" and ""Click the {} icon in the editing area."""It may not work depending on the content of the pasted text, but for your information.


2022-09-30 16:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.