我無法使用自定義SKSpriteNode按鈕在帶有3D Touch的設備上使用xCode級別編輯器。SpriteKit Xcode級別編輯器自定義按鈕類別上沒有觸摸事件(3d觸摸)
我有一個按鈕的子類,它主要基於來自蘋果的DemoBots示例。
的基本代碼將是這個
enum ButtonIdentifier: String {
case playButton
case pauseButton
}
/// Button responder delegate
protocol ButtonDelegate: class {
func pressed(button button: ButtonNode)
}
class ButtonNode: SKSpriteNode {
public weak var delegate: ButtonDelegate? {
return scene as? ButtonDelegate
}
var isHighlighted = false {
didSet {
// running skactions to colorise buttons and animate
}
}
var identifier: ButtonIdentifier!
/// Code init (when button is created in code)
/// e.g let playButton = ButtonNode(imageNamed: "ButtonImage", identifier: playButton)
init(imageNamed: String, identifier: ButtonIdentifier) {
self.identifier = identifier
let texture = SKTexture(imageNamed: imageNamed)
super.init(texture: texture, color: SKColor.clearColor(), size: texture.size())
name = identifier.rawValue
setup()
}
/// Level editor init (when button is created in level editor)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Ensure that the node has a supported button identifier as its name.
guard let nodeName = name, identifier = ButtonIdentifier(rawValue: nodeName) else {
fatalError("Unsupported button name found.")
}
self.identifier = identifier
setup()
}
private func setup() {
// zPosition
zPosition = 200
// Enable user interaction on the button node to detect tap and click events.
userInteractionEnabled = true
}
#if os(iOS)
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
isHighlighted = true
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
guard let scene = scene else { return }
for touch in touches {
let location = touch.locationInNode(scene)
let node = scene.nodeAtPoint(location)
if node === self || node.inParentHierarchy(self) {
runPressedAction()
} else {
isHighlighted = false
}
}
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
super.touchesCancelled(touches, withEvent: event)
isHighlighted = false
}
#endif
// MARK: - Pressed Action
private func runPressedAction() {
// SKAction that runs a press animation
delegate?.pressed(button: self)
}
}
如果我通過Xcode的關卡編輯器都創建按鈕在模擬器或在我的iPhone 6工作正常,但是使用測試飛行我的朋友,當用6S Plus在按鈕上沒有任何觸摸輸入,他不能按下它們。
如果我在代碼中創建了所有按鈕,即使在3D觸摸設備上也可以正常工作。
爲什麼它不能在3D觸摸設備上使用關卡編輯器中的按鈕?我一直在試圖看看蘋果演示機器人示例,看看我是否錯過了一些設置或某些東西,但我無法弄清楚。 (演示機器人按鈕做我的朋友iPhone 6s加上工作)
我知道,在3D觸摸設備touchesMoved方法被調用,即使在x/y座標沒有變化。不過,我不認爲這會導致我的問題,因爲在代碼中創建按鈕時,所有內容都按預期工作。
在Xcode級別編輯器中是否有一些設置我缺少允許觸摸3D觸摸設備?
爲什麼你會顯示不相關的代碼,顯示實際涉及觸摸事件的代碼 – Knight0fDragon
我將更新我的答案,1分鐘。主要原因是如果我用代碼創建它們,它們在3D觸摸設備上工作得很好。它只是當我使用Xcode級別編輯器,他們不工作。 – crashoverride777
也顯示那部分(如何將它添加到代碼中的場景) – Knight0fDragon