我需要檢測用戶是否點擊了一個精靈節點。Sprite節點沒有檢測到觸摸?
在這裏,我創建精靈節點:
var enemy1Texture = SKTexture(imageNamed: "enemy1")
enemy1 = SKSpriteNode(texture: enemy1Texture)
enemy1.setScale(0.3)
enemy1.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 1.2)
self.addChild(enemy1)
在
touchesBegan
在這裏,我設置變量用於檢測觸摸:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let touch = touches.first as? UITouch
let location = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
let touchedNodeName = touchedNode.name
enemy1.name = "enemy1"
enemy1.userInteractionEnabled = true
然後在函數結束時,我檢查名稱然後嘗試執行相應的代碼:
if touchedNodeName == "enemy1" {
//Enemy touched code
println("Enemy1 was touched!")
}
它不工作。當我點擊Sprite節點向下移動屏幕時,沒有任何內容被打印到控制檯上。我究竟做錯了什麼?
你是右命名didMoveToView中的節點是問題的一半,另一半是我正在檢查節點是否在已經滿足的if-else語句的END處被輕敲,因此它沒有運行用於觸摸節點。謝謝! – skyguy