我正在尝试获取SceneKit中触摸位置的条件。我触摸SpriteKit对象,并编写该代码以获取SKnode对象:
class GameViewController: UIViewController , SCNSceneRendererDelegate {
var overlay: SKScene!
...
override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent!) {
for touch: AnyObject in touches {
let location: CGPoint = touch.location(in: overlay)
let touchedNode: SKNode = overlay.nodes(at: location
if(touchedNode.name == "WalkAnimationButton") {
self.tap()
}
else if(touchedNode.name == "CameraButton") {
sceneView.allowsCameraControl = true
}
else if(touchedNode.name == "WrenchButton") {
sceneView.showsStatistics = true
}
else if(touchedNode.name == "CharacterButton") {
if(currentCharacter < characterPaths.count-1) {
currentCharacter = currentCharacter+1
}
else{
currentCharacter = 0
}
}
}
self.setCharacterFromModelWithName(name: characterPaths[currentCharacter] as! NSString)
}
Xcode编写了我从未遇到过的错误:
无法将类型“[SKNode]”的值转换为指定的类型“SKNode”
最佳答案
要调用被触摸的节点,我们应该使用该代码:
let touchedNode: SKNode = overlay.atPoint(location)
因为我们调用SpriteKit对象的名称,它用作SKNode类的成员。
关于compiler-errors - SpriteKit Swift3接触了SKNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43305756/