2016-07-14 11 views
0

나는 구멍에 공을 떨어 뜨리려고하는 간단한 spritekit 게임을 만들고 있습니다. 화면 상단에 "왼쪽 공 : #"및 "점수 : #"라는 2 개의 레이블이 있습니다. touchesBegan 메서드을 사용하여 볼이 화면을 터치하는 지점에서 드롭해야 함을 알립니다. 그들이 스크린을 만질 때, 나는 또한 그들이 남긴 공의 수를 줄이고 "공 왼쪽"레이블을 업데이트하려고합니다. 그러나 동시에 볼을 떨어 뜨리고 볼 수를 하나씩 줄이는 코드를 구현하려고하면 볼이 나타나지 않지만 점수 레이블이 올바르게 업데이트됩니다. 라벨 업데이트가 공을 떨어 뜨리는 기능을 무시하기 때문에 이것이라고 생각합니다. 이 문제를 해결하는 방법에 대한 권장 사항은 환상적입니다! 내 코드가 아래에 있습니다! 미리 :) 감사드립니다touchesBegan() 제대로 작동하지 않음

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

     for touch in touches { 

      let ball = SKSpriteNode(imageNamed:"ball") 

      ball.xScale = 0.04 
      ball.yScale = 0.04 
      ball.position = touch.locationInNode(self) 


      ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2) 

      self.addChild(ball) 
     } 


    } 

이 내 viewController.swift에있는 코드 내 GameScene.swift에있는 코드

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     ballsLeftInt -= 1 
     testEndGame() 
    } 

답변

0

당신에게 총 볼 번호를 사용하여 생성 된 볼 수를 제한 할 수 있습니다. 플레이어가 각 게임에서 10 개의 공을 떨어 뜨리기를 원한다고 가정하십시오. 당신의 GameScene.swift에서 설정 전역 값

var ballNum = 10 

그런 다음 TouchesBegin 방법

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

    if ballNum > 0 { 

     for touch in touches { 

      let ball = SKSpriteNode(imageNamed:"ball") 

      ball.xScale = 0.04 
      ball.yScale = 0.04 
      ball.position = touch.locationInNode(self) 


      ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2) 

      self.addChild(ball) 
      ballNum -= 1 
      //tell VC to update the two labels in your VC 
     } 

    }else { 
     //tell VC game over and reload ballNum after user click replay 
    } 
} 

NSNotificationCenter와 관찰자를 사용하여 달성 할 수 VC를 알 수있는 방법으로 업데이트합니다.

그래서 VC의보기가로드되면 두 개의 옵저버 (하나는 업데이트 UI 라벨 용이고 다른 하나는 게임 오버 용)를 추가해야합니다. 벤처 캐피탈에서

, 마지막 방법은 다시 게시물 알림에 의해 VC 말할 수 touchesBegan 방법을 얻을, 이제

func updateScore() { 
    // update your labels 
} 

func informGameOver() { 
    // tell the player game over and offer the play again button 
} 

을 viewDidLoad에

override func viewDidLoad() { 
    super.viewDidLoad() 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.updateScore), name: "updateScore", object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.informGameOver), name: "informGameOver", object: nil) 

} 

를 다시 그리고 두 가지 기능을 추가 같은

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

if ballNum > 0 { 

    for touch in touches { 

     let ball = SKSpriteNode(imageNamed:"ball") 

     ball.xScale = 0.04 
     ball.yScale = 0.04 
     ball.position = touch.locationInNode(self) 


     ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2) 

     self.addChild(ball) 
     ballNum -= 1 
     NSNotificationCenter.defaultCenter().postNotificationName("updateScore", object: nil) 
    } 

}else { 
      NSNotificationCenter.defaultCenter().postNotificationName("informGameOver", object: nil) 

} 

}

+0

도움을 주신 것에 대해 충분히 감사 드릴 수는 없으며 NSNotificationCenter 참조가 존재하는지조차 알지 못했습니다. 불행히도 이것은 내가 가지고있는 문제를 해결하지 못했습니다. 화면에서 탭하면 볼이 나타나지 않습니다. 그러나 라벨 업데이트는 계속 진행 중입니다. –

+0

@jj_hennessy 예, 예를 들어이 답변을이 질문의 정답으로 표시하면 질문을 보는 사람들이 즉시 해결 된 행운을 볼 수 있습니다. –