如何防止布尔值在if语句中被覆盖?(How do I prevent Boolean values to be overwritten in if statement?)

编程入门 行业动态 更新时间:2024-10-19 15:38:42
如何防止布尔值在if语句中被覆盖?(How do I prevent Boolean values to be overwritten in if statement?)

我试图通过检查布尔值以确定if语句中的状态来通过键删除SKAction。

我试图实现的解决方案的原因是为了让最终用户能够在应用程序中将FX音效静音。

声明:

var mute: Bool? = false let popAction = SKAction.playSoundFileNamed("flaps", waitForCompletion: false) let coinAction = SKAction.playSoundFileNamed("coinSound", waitForCompletion: false)

If语句在采取行动之前确定值,以及在if语句正在运行(即使bool为真)之后问题可能出现的位置:

if !self.mute! { // Run when Bool is false self.run(self.coinAction, withKey: "coinSound") print("Bool Value: '\(self.mute)'") }

布尔值:错误

当Bool为真时删除SKAction的函数:

func muteSound() -> Bool { self.mute = !self.mute! removeAction(forKey: "coinSound") print("Bool Value: '\(self.mute)'") return true }

Bool值:是的

运行功能:

muteSound()

请注意,此功能是通过主菜单上的按钮执行的。

如果你能花点时间看看我是否错过了任何会导致这个问题的事情或做过任何事情,我将不胜感激。

I'm trying to remove SKAction with key through checking the Bool value to determine state in an if statement.

The reason for this solution I'm trying to achieve is to give the end-user the ability to mute the FX sounds in the app .

Declaring:

var mute: Bool? = false let popAction = SKAction.playSoundFileNamed("flaps", waitForCompletion: false) let coinAction = SKAction.playSoundFileNamed("coinSound", waitForCompletion: false)

If statement to determine value before taking action and also where the issue might be since the if statement is running even if bool is true:

if !self.mute! { // Run when Bool is false self.run(self.coinAction, withKey: "coinSound") print("Bool Value: '\(self.mute)'") }

Bool Value: false

Function to remove SKAction when Bool is true:

func muteSound() -> Bool { self.mute = !self.mute! removeAction(forKey: "coinSound") print("Bool Value: '\(self.mute)'") return true }

Bool Value: True

Run function:

muteSound()

Note this function is executed through a button on the main menu.

Would appreciate if you could take your time and see if I have missed anything or done anything that would cause this issue.

最满意答案

在这一点上,你有两个选择

你可以解开你的“静音”布尔类似

var mute: Bool = false

你不需要添加“?” 因为你初始化“静音”为假(而不是零(我推断,你永远不会分配“静音”为零))。

如果你想保留“?” 你必须打开“静音”然后测试它

if let mute = self.mute, !self.mute { // Run when Bool is false self.run(self.coinAction, withKey: "coinSound") print("Bool Value: '\(self.mute)'") }

概要

At this point, you have two choices

You can unwrap your "mute" boolean like

var mute: Bool = false

You don't need to add a "?" because you initialize "mute" to false (and not to nil (i extrapolate that you never assign "mute" as nil)).

If you want to keep the "?" you must unwrap "mute" and then test it

if let mute = self.mute, !self.mute { // Run when Bool is false self.run(self.coinAction, withKey: "coinSound") print("Bool Value: '\(self.mute)'") }

summary

更多推荐

本文发布于:2023-07-26 15:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被覆   语句   如何防止   布尔值   prevent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!