如何在场景转换尝试中处理“无场景”?

时间:2016-12-07 05:22:22

标签: swift sprite-kit skscene

在一个应该加载一个场景的按钮中,我正在尝试学习使用一个guard语句,但是对它在四个“逃逸”中的每一个中的作用感到非常困惑。并且不知道在处理没有场景的情况时我应该做些什么。

这里使用的正确无误: continuereturnbreakthrow

而且......为什么?

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch: AnyObject in touches {

        let location = touch.location(in: self)

        if self.rr.contains(location) {
            guard let nextScene = goesTo
                else {print(" No such Scene ")
                continue } // continue, return, break, throw !!!!????
            loadScene(withIdentifier: nextScene)
            }
        }
    }

3 个答案:

答案 0 :(得分:5)

我不认为风景适合警卫声明,因为你有许多情况下循环,你想避免一些情况!首先,我们需要知道何时应该使用保护声明。是的,它可以帮助您处理错误,但这并不意味着您应该随时随地使用它!

  

为什么要警惕以及何时使用它们而不是使用它们:

     

The Guard Statement in Swift

<强>更新

我希望这能澄清你的问题

  

当你使用break

let array: [Int?] = [3, 7, nil, 12, 40]

for arrayValue in array {

    guard let value = arrayValue else {
        print("No Value")
        break
    }

    print(value)
}

<强>输出

3

7

无价值

  

当你使用继续

let array: [Int?] = [3, 7, nil, 12, 40]

for arrayValue in array {

    guard let value = arrayValue else {
        print("No Value")
        continue
    }

    print(value)

<强>输出

3

7

无价值

12

40

返回将关闭该函数,并且在这种情况下将与中断相同。现在请根据您的需要做出决定!如果您认为在没有场景条件时可以打破它,那么中断或者如果您想跳过它,那么只需跳过确切的场景即可。我希望你明白我的观点

答案 1 :(得分:3)

如果您处于for循环中,则continue将移至下一次迭代,而break将退出for循环。 Return将始终退出当前函数。

在这种情况下,您可能希望在for循环之前放置guard语句并退出touchesEnded,因为我假设goesTo设置在别处。

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let nextScene = goesTo else {
        print(" No such Scene ")
        return // exit touchesEnded since goesTo is not defined 
    }

    for touch: AnyObject in touches {
        let location = touch.location(in: self)

        if self.rr.contains(location) {
            loadScene(withIdentifier: nextScene)
        }
    }
}

答案 2 :(得分:2)

在你的特定例子中,所有三个都会做同样的事情。但是在touchesEnded中使用循环是很奇怪的,因为它是最后一次触摸,只有一次触摸,除非你有多重感染。

如果您要在for循环下添加其他代码,那么您可能会破坏您的程序,因为您没有处理guard语句。如果你想在没有场景的情况下忽略函数的其余部分,那么使用return将是理想的。

// Break works with named labels, which can be used with while, do, and for.
// Why? Break exits loops because it's a command that redirects the
// execution of code, similar to goto in C.

outerLabel: do {
    if 0==0 { print("numbers are fun and,") }
    if 0==2 { break outerLabel } // NOTE: the label is required for
                                 // `break` when not in `while` or `switch`
    print("zero was not equal to two")
}

// Continue breaks from the current iteration (whatever value `i` is at),
// but then restarts the loop with the next value (0..1..2..3...)
for i in 1...10 {
  print(" Hello ")
  if 0==0 { continue }
  print(" Worlllddd ") // We will see 10 hellos but no worlds.
}


// Here is the example with guard
stuff: do {
  guard 0 == 2 else { break stuff } // Again, the label here is required for break.
  print(" zero is equal to two " ) // Doesn't print, lol...
}

moreStuff: for i in 1...10 {
  print(" is zero equal to two? ")
  guard 0 == 0 else { continue } // Label is optional for continue.
  print(" zero is equal to zero " ) // Does print.
}

// Here is how we use return with guard, and the print maybe explains why:
func ohLookAFunc() {
  guard 0 == 0 else {
    print(" Math and numbers as we know it are over!! ")
    return
    // ... now we xit from this crazy Func where 0 is not 0
    // You can handle this exception in any-way you see fit.
    // So if your scene doesn't exist, or if 0 is == 2, then 
    // it's up to you to figure out what to do in such disastrous
    // situations ;)
  }
  print(" Now we continue the function safe in the fact that 0 is still equal to 0")
}

没有最好的&#34;使用这些陈述。这取决于正在做什么以及设置的逻辑。

如果没有找到场景,没有人可以告诉你该做什么,只是忽略输入(从touchesEnded()返回而不做任何事情)。

或者,您可以将其设置为确保始终显示正确的场景。那部分是关于你:)没有更多的代码,我们无法帮助你确保。