cdp*_*mer 4 switch-statement ios sprite-kit skscene swift
我想清理我touchesBegan(..)的SKScene.我想做一个case语句而不是我的if .. else链.但是,我在实现时遇到错误,这表明我不知道如何在引擎盖下完成相同的操作.
代码之前:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) === playLabel {
buildLevelSelect()
} else if self.nodeAtPoint(location) === aboutLabel {
createAboutView()
} else if self.nodeAtPoint(location) === storeLabel {
createStore()
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码之后:点击后,某些标签点击工作,但其他一些引发Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode 0x0)错误:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
switch(self.nodeAtPoint(location)){
case playLabel :
buildLevelSelect()
case aboutLabel :
createAboutView()
case storeLabel :
createStore()
default : break
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
switch如果你想要的===行为如下,你可以编写一种奇怪但看起来很有用的东西:
switch(true){
case self.nodeAtPoint(location) === playLabel : buildLevelSelect()
case self.nodeAtPoint(location) === aboutLabel : createAboutView()
case self.nodeAtPoint(location) === storeLabel : createStore()
default : break
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这仍然比if-else链更清晰.
| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |