我试图通过检查布尔值以确定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)'") }
更多推荐
发布评论