2017-07-28 44 views
2

我有一個菜單按鈕(skSpriteNode)和小遊戲圖標,這個精靈裏面spritekit使childnode沒有可觸摸

mainButton.addChild (playIcon) 
mainButton.name = "xyz" 
addchild (mainButton) 

好吧,我給該按鈕的名稱,以檢查是否有此名稱的精靈被觸摸。

當我觸摸按鈕內的圖標子時,touchedNode.name爲零。我爲圖標子設置了isUserInteractionEnabled = false。我想通過觸摸到父母。我怎樣才能做到這一點。

for touch in touches { 

      let location = touch.location(in: self) 
      let touchedNode = self.atPoint(location) 

      if (touchedNode.name != nil) { 
       print ("node \(touchedNode.name!)") 
      } else { 
       continue 
      } 
} 

回答

4

你必須在SKSpriteNode子類來實現touchesBegan並設置其isUserInteractionEnabled = true,以接受和處理對特定節點(按鈕)的觸摸。現在

import SpriteKit 

protocol ButtonDelegate:class { 

    func printButtonsName(name:String?) 
} 

class Button : SKSpriteNode{ 

    weak var delegate:ButtonDelegate? 

    init(name:String){ 
     super.init(texture: nil, color: .purple, size: CGSize(width: 250, height: 250)) 
     self.name = name 
     self.isUserInteractionEnabled = true 
     let icon = SKSpriteNode(color: .white, size: CGSize(width:100, height:100)) 
     addChild(icon) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 


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



     delegate?.printButtonsName(name: self.name) 
    } 
} 

class GameScene: SKScene, ButtonDelegate { 

    override func didMove(to view: SKView) { 

     let button = Button(name:"xyz") 
     button.delegate = self 


     button.position = CGPoint(x: frame.midX - 50.0, y: frame.midY) 


     addChild(button) 

    } 

    func printButtonsName(name: String?) { 

     if let buttonName = name { 
      print("Pressed button : \(buttonName) ") 
     } 
    } 
} 

,這個按鈕將吞下的觸摸和圖標精靈將被忽略。

我剛剛修改了我的答案one中的代碼,以便根據您的需求製作一個示例,但如果您對此感興趣,則可以在該鏈接中詳細閱讀有關整個事項的詳細信息。

+0

謝謝,我學到了一些東西。如何檢查觸摸位置是否在透明像素上?當我有兩個帶無形窗體的按鈕,但重疊的矩形區域時,單擊zposition中的第一個按鈕。 – bluelemonade

+0

@bluelemonade這可以通過至少兩種方式解決(例如,使用CGImage提取像素阿爾法值)或製作一個不規則形狀的物理體(這實際上取決於形狀)並使用PhysicsWorld的'bodyAt(point:)'方法。看看這個:https://stackoverflow.com/a/20932809/3402095 – Whirlwind

+0

好吧,我可以檢測到紋理的alpha。但觸摸事件並沒有向底層按鈕「冒泡」。 – bluelemonade

1

我實現了按鈕Touchbegan,並試圖爲具有背景的滑塊和滑塊內的子滑塊實現子類。我在我的GameScene中添加了sliderDelegate Protocoll。向下觸摸是好的,但我沒有得到移動

import Foundation 
import SpriteKit 


protocol SliderDelegate:class { 

    func touchesBeganSKSpriteNodeSlider(name:String?, location: CGPoint?) 
    func touchesMovedSKSpriteNodeSlider(name:String?, location: CGPoint?) 
    func touchesEndedSKSpriteNodeSlider(name:String?, location: CGPoint?) 

} 


class SKSpriteNodeSlider : SKSpriteNode{ 

    weak var delegate:SliderDelegate? 

    init(imageNamed: String){ 

     // super.init(imageNamed: imageNamed) 
     let texture = SKTexture(imageNamed: imageNamed) 
     super.init(texture: texture, color: .white, size: texture.size()) 
     // self.name = name 
     self.isUserInteractionEnabled = true 
     // let icon = SKSpriteNode(color: .white, size: CGSize(width:100, height:100)) 
     // addChild(icon) 
    } 

    required init?(coder aDecoder: NSCoder) { 

     fatalError("init(coder:) has not been implemented") 

    } 


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

     // for touch in touches { 
     if let touch = touches.first { 
      let loc = touch.location(in: self) 
      delegate?.touchesBeganSKSpriteNodeSlider(name: self.name, location: loc) 

     } 

    } 


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

     print ("touchesMoved") 

     // for touch in touches { 
     if let touch = touches.first { 
      let loc = touch.location(in: self) 
      delegate?.touchesMovedSKSpriteNodeSlider(name: self.name, location: loc) 
     } 

    } 


    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if let touch = touches.first { 
      let loc = touch.location(in: self) 
      delegate?.touchesEndedSKSpriteNodeSlider(name: self.name, location: loc) 
     } 

    } 

}