2016-06-17 4 views
0

swift와 spritekit으로 작은 게임을 만들고 현재 메뉴 작업을하고 있으며 SKButtonNode라는 사용자 정의 Buttonclass를 SKNode의 하위 클래스 인 Button으로 만들었습니다. 내 휴대 전화에서 Button을 클릭하면 "userInteractionEnabled = true"를 사용하더라도 아무 일도 일어나지 않습니다. 그런데 왜 내가 버튼을 만졌을 때 "test2"를 볼 수는 없지만 버튼 옆에 닿는다면 그것을 볼 수 있습니까?Swift/Spritekit 터치시 SKNode가 감지되지 않습니다.

class SKButtonNode: SKNode { 
    var defaultButton: SKSpriteNode 
    var activeButton: SKSpriteNode 
    var action:() -> Void 

    init(defaultButtonImage: String, activeButtonImage: String, buttonAction: () -> Void) { 
     defaultButton = SKSpriteNode(imageNamed: defaultButtonImage) 
     activeButton = SKSpriteNode(imageNamed: activeButtonImage) 
     activeButton.hidden = true 
     action = buttonAction 

     super.init() 

     userInteractionEnabled = true 
     addChild(defaultButton) 
     addChild(activeButton) 
    } 

class MenuScene: SKScene, SKPhysicsContactDelegate { 

var startGameButton: SKButtonNode! 
var optionsGameButton: SKButtonNode! 
var exitGameButton: SKButtonNode! 


// Update time 
var lastUpdateTimeInterval: NSTimeInterval = 0 


override func didMoveToView(view: SKView) { 
    // Setup physics world's contact delegate 
    physicsWorld.contactDelegate = self 
    let startGameButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame) 

    startGameButton.position = CGPoint(x: 640, y: 330) 
    startGameButton.activeButton.size = CGSize(width: 360, height: 95) 
    startGameButton.defaultButton.size = CGSize(width: 360, height: 95) 
    startGameButton.name = "startGameButton" 


    let startOptionsButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame) 

    startOptionsButton.position = CGPoint(x: 640, y: 210) 
    startOptionsButton.activeButton.size = CGSize(width: 360, height: 95) 
    startOptionsButton.defaultButton.size = CGSize(width: 360, height: 95) 
    startOptionsButton.name = "startOptionsButton" 

    let exitGameButton = SKButtonNode(defaultButtonImage: "blue_button_up", activeButtonImage: "blue_button_down" , buttonAction: startGame) 

    exitGameButton.position = CGPoint(x: 640, y: 90) 
    exitGameButton.activeButton.size = CGSize(width: 360, height: 95) 
    exitGameButton.defaultButton.size = CGSize(width: 360, height: 95) 
    exitGameButton.name = "exitGameButton" 

    addChild(startGameButton) 
    addChild(startOptionsButton) 
    addChild(exitGameButton) 


} 
func exitGame() { 

} 

func startOptions(){ 

} 

func startGame() { 
    print("test") 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let location = touches.first!.locationInNode(self) 
    let node = self.nodeAtPoint(location) 
    print("test2") 
    if (node.name == "startGameButton") { 
     let currentNode = node as! SKButtonNode 
     currentNode.activeButton.hidden = false 
     currentNode.defaultButton.hidden = true 
    } 

} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let location = touches.first!.locationInNode(self) 
    let node = self.nodeAtPoint(location) 
    if (node.name == "startGameButton") { 
     let currentNode = node as! SKButtonNode 
     if currentNode.defaultButton.containsPoint(location) { 
      currentNode.activeButton.hidden = false 
      currentNode.defaultButton.hidden = true 
     } else { 
      currentNode.activeButton.hidden = true 
      currentNode.defaultButton.hidden = false 
     } 
    } 

} 


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let location = touches.first!.locationInNode(self) 
    let node = self.nodeAtPoint(location) 
    print("test3") 
    if (node.name == "startGameButton") { 
     let currentNode = node as! SKButtonNode 
     if currentNode.defaultButton.containsPoint(location) { 
      startGame() 
     } 
     currentNode.activeButton.hidden = true 
     currentNode.defaultButton.hidden = false 
    } 
} 
+0

여기서 (코드에서) touchesBegun, touchesMoved 및 touchesEnded가 있습니까? – Confused

답변

1

실제로는 버튼 동작을 실행하지 않는 것처럼 보입니다. touchesBegan에서 버튼을 눌렀을 때 수행중인 모든 작업은 속성을 true 또는 false으로 설정하는 것입니다. 다음과 같이 시도하십시오.

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

    let location = touches.first!.locationInNode(self) 
    let node = self.nodeAtPoint(location) 

    print("test2") 

    if (node.name == "startGameButton") { 
     let currentNode = node as! SKButtonNode 

     self.runAction(currentNode.action) 

     currentNode.activeButton.hidden = false 
     currentNode.defaultButton.hidden = true 
    } 
} 
+0

나는 결코 touchesBegan 메서드에 들어 가지 않습니다. 내 문제입니다. – Mikinho90

+0

touchesBegan 메서드의 꼭대기에 print 문이나 중단 점을 넣었습니까? – claassenApps

관련 문제