2017-04-22 1 views
-1

마지막 레이블에 lblTryAgain GameScene이라는 다른 클래스로 돌아가고 싶지만 tap 액션이 touchesBegan 함수에 입력되지 않습니다. 난 그냥 누군가가 튜토리얼을 따라하거나 전체 코드를보고 싶다면 SpriteKit와 게임을 개발하는 방법을 배울 수있는 튜토리얼을 다음입니다 것은 https://www.raywenderlich.com/87231/make-game-like-mega-jump-sprite-kit-swift-part-1TouchesBegan이 작동하지 않습니다.

import SpriteKit 

class EndGameScene: SKScene { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override init(size: CGSize) { 
     super.init(size: size) 

     // Stars 
     let star = SKSpriteNode(imageNamed: "Star") 
     star.position = CGPoint(x: 25, y: self.size.height-30) 
     addChild(star) 

     let lblStars = SKLabelNode(fontNamed: "ChalkboardSE-Bold") 
     lblStars.fontSize = 30 
     lblStars.fontColor = SKColor.white 
     lblStars.position = CGPoint(x: 50, y: self.size.height-40) 
     lblStars.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left 
     lblStars.text = String(format: "X %d", GameState.sharedInstance.stars) 
     addChild(lblStars) 

     // Score 
     let lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold") 
     lblScore.fontSize = 60 
     lblScore.fontColor = SKColor.white 
     lblScore.position = CGPoint(x: self.size.width/2, y: 300) 
     lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center 
     lblScore.text = String(format: "%d", GameState.sharedInstance.score) 
     addChild(lblScore) 

     // High Score 
     let lblHighScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold") 
     lblHighScore.fontSize = 30 
     lblHighScore.fontColor = SKColor.cyan 
     lblHighScore.position = CGPoint(x: self.size.width/2, y: 150) 
     lblHighScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center 
     lblHighScore.text = String(format: "High Score: %d", GameState.sharedInstance.highScore) 
     addChild(lblHighScore) 


     // Try again 
     let lblTryAgain = SKLabelNode(fontNamed: "ChalkboardSE-Bold") 
     lblTryAgain.fontSize = 30 
     lblTryAgain.fontColor = SKColor.white 
     lblTryAgain.position = CGPoint(x: self.size.width/2, y: 50) 
     lblTryAgain.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.center 
     lblTryAgain.text = "Tap To Try Again" 
     lblTryAgain.isUserInteractionEnabled = true 
     addChild(lblTryAgain) 


    } 

    func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
     // Transition back to the Game 
     let reveal = SKTransition.fade(withDuration: 0.5) 
     let gameScene = GameScene(size: self.size) 
     self.view!.presentScene(gameScene, transition: reveal) 
    } 

} 
+0

당신의 방법 터치 isUserInteractionEnabled이 선언은 잘못 시작했다 인쇄 –

+4

'touchesBegan이 FUNC 오버라이드 (_ 닿은 : 이벤트와, 설정 : UIEvent를) {' –

+0

당신이 빠른 3에 노력하고 있습니다? –

답변

1

touchesBegan에서 사용할 수 있습니다 만 self.view과 작동하는 재정 func입니다 다른 UI를 에서 작동하지 않습니다 것은이

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

    print("working") 
} 
+0

'touchesBegan'은 우선 적용되는 메소드입니다. 재정의 된 함수가 아닙니다. 클래스에서이 메서드를 ovverride하여 뷰 또는 창에서 새로운 터치가 감지되는지 확인할 수 있습니다. 그리고 그것은'self.view'에 국한되지 않습니다 .. –

1

isUserInteractionEnabled가 체크되어 있는지 확인 시도하고 print 문이 작동하지 않을 수 있습니다. 추가하거나 sceneDidLoad를 업데이트하고

관련 문제