How to delete a button when the "Game" condition is met

Asked 1 years ago, Updated 1 years ago, 66 views

I'm a beginner at Swift.We are currently trying to develop a simple game application, with 5 buttons (provisional) on the screen, a concept of winning or losing, and when you tap on the outside, the image and sound of the game are output, which is like a black beard crisis.

実装Feature 1: I want to remove the button when I tap the button that I want to implement >
This will make it easier to visually understand that the off button will remain, and it will make the player impatient.

実装Function 2: I want to stop the BGM starting right after tapping the button that came off の
Please let me know if using .stop() in the if statement doesn't work.

import UIKit

import AVFoundation
classViewController:UIViewController {
var player —AVAudioPlayer!
var doll —AVAudioPlayer!//doll is a button

// 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)

    try!player=AVAudioPlayer(contentsOfURL:url)
    player.numberOfLoops=-1
    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.


   // Sound that comes when you tap a hit
    let url = NSBundle.mainBundle().URLForResource("waraiufufu", withExtension: ".wav")!
    try!doll=AVAudioPlayer(contentsOfURL:url)
    doll.prepareToPlay()


// Sound that comes when you tap the missing button
lethazureURL=NSBundle.mainBundle().URLForResource("gyaa", 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

    }
}
}

swift xcode

2022-09-30 21:16

1 Answers

Feature 1: I want to remove the button when I tap the button per person
There is a way to remove it from the actual view hierarchy, but if you just want to erase it from the perspective, why don't you manipulate the hazureImage property to hide it?(If you delete a child view containing control parts such as UIButton from the view hierarchy that you set up once, the app may drop due to unexpected side effects.You shouldn't "actually delete" it until you have a good understanding of that and can handle it yourself.)

Feature 2: I want to stop the BGM from starting right after tapping the button that came off
Usually, if you call the stop() method (there is a little lag until it actually stops), the sound during the performance stops...However, if you stop at stop(), the next time you call play(), the equivalent of prepareToPlay() runs again, which increases the lag until the start of the performance.

So if you insert the above two points into your code, the dolltapped(_:) method looks like this.

@IBAction func dolltapped (sender:UIButton) {
        if sender.tag!=hazureTag{
            doll.play()
            sender.hidden=true// Hide correct button
        } else{
            hazurePlayer.play()
            hazureImage.hidden=false
            player.pause() // Stop BGM if it's off
        }
    }

(If you want to use the same ViewController to view the following issues, you should include the following actions before displaying the hidden property of all buttons to false etc.)

problem.)

If stop() doesn't work well, I don't think pause() will work, but if it doesn't work, please contact us via comments.


2022-09-30 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.