2014-06-08 5 views
14
목표 - C에서

에 장면을 전환하는 방법, 내가 목표 - C에서 다음 코드와 같은 성공적으로 사용 뭔가하려고에서 새로운 장면는 스프라이트 키트를 사용하여 신속한

if ([touchedNode.name isEqual: @"Game Button"]) { 
     SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0]; 
     GameScene *newGameScene = [[GameScene alloc] initWithSize: self.size]; 
     // Optionally, insert code to configure the new scene. 
     newGameScene.scaleMode = SKSceneScaleModeAspectFill; 
     [self.scene.view presentScene: newGameScene transition: reveal]; 
    } 

을 기르는 것 스위프트 내 간단한 게임 포트, 지금까지 나는이 작업 ...

for touch: AnyObject in touches { 
     let touchedNode = nodeAtPoint(touch.locationInNode(self)) 
     println("Node touched: " + touchedNode.name); 
     let touchedNodeName:String = touchedNode.name 

     switch touchedNodeName { 
     case "Game Button": 
      println("Put code here to launch the game scene") 

     default: 
      println("No routine established for this") 
     } 

을하지만 실제로 다른 장면으로 전환에 쓸 어떤 코드 모른다. 질문 :

  1. 누군가 Swift에서 SKTransition을 사용하는 예를 제공해 줄 수 있습니까?
  2. 대개 다른 장면 코드를 다른 장면에 넣으려고합니까? Objective-C 아래 있다고 가정 할 때, 아니면 다르게 접근해야 함을 의미하는 Swift 사용에 대해 뭔가가 있습니까?

그것은의 당신에게 달려 종류의, 당신은

+1

2 스위프트 실제로는 자동적으로 모듈에있는 모든 파일을 수입하기 때문에 쉽게 여러 개의 파일을 사용할 수있게 . Objective-C –

답변

38

두 번째 질문으로 시작하려면 감사드립니다. 원한다면 파일마다 하나의 클래스를 갖는 Objective-C 협약을 계속할 수 있습니다. 이는 요구 사항은 아니지만 Objective-C에도 없었습니다. 즉, 밀접하게 관련되어 있지만 많은 코드로 구성되지 않은 몇 가지 클래스가있는 경우 단일 파일로 그룹화하는 것이 무리가 아닐 수 있습니다. 그냥 옳은 것을하고, 하나의 파일에 거대한 코드를 만들지 마라.

첫 번째 질문에 대해서는 ... 이미 그렇습니다. 기본적으로, 어디서 왔는지에 따라 GameScene 인스턴스를 크기 (이니셜 라이저)를 통해 만들어야합니다. 거기에서 속성을 설정하고 present를 호출하면됩니다. 당신은 터치 begain 또는 노드 액션에서 작업해야하는 경우

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

    guard let location = touches.first?.locationInNode(self), 
     let scene = scene, 
     nodeAtPoint(location) == "Game Button" else { 
     return 
    } 

    let transition = SKTransition.reveal(
     with: .down, 
     duration: 1.0 
    ) 

    let nextScene = GameScene(size: scene.size) 
    nextScene.scaleMode = .aspectFill 

    scene.view?.presentScene(nextScene, transition: transition) 
} 
+2

과 같은 패턴을 따르지 않는 이유는 없습니다. 고맙습니다. GameScene (크기 : self.scene.size)의 "GameScene"은 생성 한 SpriteKit 파일 이름을 나타냅니다 (특별한 예약어는 아닙니다). . – Narwhal

+1

전환하는 장면은 어떻게됩니까? 계속 실행 되나요? 아니면 의도적으로 일시 중지해야합니까? 씬에서 어떤 액션이 일어나고 있다면 그 액션은 당신의 게임에 영향을 미칠 수 있습니다. – zeeple

+2

@ user1639164 기본적으로 전환 중에 나가는 장면과 들어오는 장면이 모두 일시 중지됩니다. 이 동작을 변경하려면 SKTransition의'pausesIncomingScene' 및'pausesOutgoingScene' 속성 값을 변경하여 수행 할 수 있습니다. –

3

, 그리고 그것을 사용 :

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { 
     let touch = touches.anyObject() as UITouch 
     if CGRectContainsPoint(btncloseJump.frame, touch.locationInNode(self)) { 
      self.scene.removeFromParent() 
      btncloseJump.removeFromParent() 
      let skView = self.view as SKView 
      skView.ignoresSiblingOrder = true 
      var scene: HomeScene! 
      scene = HomeScene(size: skView.bounds.size) 
      scene.scaleMode = .AspectFill 
      skView.presentScene(scene, transition: SKTransition.fadeWithColor(SKColor(red: 25.0/255.0, green: 55.0/255.0, blue: 12.0/255.0, alpha: 1), duration: 1.0)) 
     } 
    } 
+0

이 대답은 나를 도왔습니다. – Nate4436271