-4
因此,在我的代碼中,我想添加一個SKSpriteNode,我可以將它用作按鈕以轉換到另一個場景,所以,如果不使用gameScene .sks我想在我的遊戲中使用swift添加按鈕
因此,在我的代碼中,我想添加一個SKSpriteNode,我可以將它用作按鈕以轉換到另一個場景,所以,如果不使用gameScene .sks我想在我的遊戲中使用swift添加按鈕
一般情況下,你需要創建一個子節點首先
override init(size: CGSize){
let playButton = SKLabelNode()
playButton.fontSize = 40
playButton.text = NA_MENU_BTN_PLAY
playButton.position = CGPoint(x: self.frame.midX, y: 300)
//give it an unique name
playButton.name = "Play"
playButton.zPosition = 3
addChild(playButton)
}
然後使用func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
處理觸摸事件
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches){
let location = touch.location(in: self)
let touchNode = atPoint(location)
if !((touchNode.name) != nil) { return }
switch touchNode.name! {
case "Play" :
//your logic here!!
let trialScene = TrialScene(size: self.size)
self.view! .presentScene(trialScene, transition: transition)
}
}
}