如何在Sprite Kit中切换场景?

时间:2015-03-21 18:27:23

标签: swift sprite-kit

我的代码出了什么问题?每次我按下按钮,我都会得到一个默认的灰色屏幕。过渡按钮:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
            var pauseScene = PauseScene(size: self.size)
            scene?.paused = true
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            pauseScene.scaleMode = .AspectFill
            pauseScene.size = skView.bounds.size
            skView.presentScene(pauseScene)
        }
    }
}

我在该场景中不想加载的内容:

class PauseScene: SKScene {
    let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")

    override func didMoveToView(view: SKView) {
        self.pauseBackground.anchorPoint = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        self.addChild(pauseBackground)
    }

}

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您的问题似乎在PauseScene的didMoveToView:中。您不想更改self.pauseBackground的定位点。通常,锚点基于0到1(.5,.5)作为精灵的中心。你将它设置为远高于此值的东西,如果你将位置设置为场景宽度和高度的一半,我就不明白为什么你想要改变锚点。看起来只是删除代码行应该解决问题。

class PauseScene: SKScene {
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")

    override func didMoveToView(view: SKView) {
        self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        self.addChild(pauseBackground)
    }
}

此外,我发现您在启动后会改变场景的大小。我只想用你想要的尺寸初始化它。

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.pause {
            let skView = self.view as SKView!
            skView.ignoresSiblingOrder = true
            var pauseScene = PauseScene(size: skView.bounds.size)
            scene?.paused = true
            pauseScene.scaleMode = .AspectFill
            skView.presentScene(pauseScene)
        }
    }
}

希望这有帮助并且有意义。

答案 1 :(得分:0)

这是一个更新的答案 swift 5, iOS 14 Using swiftUI

导入 SwiftUI 导入 SpriteKit

struct ContentView: View {

@State var switcher = false

var scene: SKScene {
    let scene = GameScene.shared
    scene.size = CGSize(width: 256, height: 256)
    scene.scaleMode = .fill
    scene.backgroundColor = .red
    scene.name = "red"
    return scene
}

var scene2: SKScene {
    let scene2 = GameScene2.shared
    scene2.size = CGSize(width: 256, height: 256)
    scene2.scaleMode = .fill
    scene2.backgroundColor = .blue
    scene2.name = "blue"
    return scene2
}

var body: some View {
  if switcher {
    SpriteView(scene: scene)
        .frame(width: 256, height: 256)
        .ignoresSafeArea()
        .background(Color.red)
        .onAppear {
          scene2.isPaused = true
        }
        .onDisappear {
          scene2.isPaused = false
        }
        
  } else {
    SpriteView(scene: scene2)
        .frame(width: 256, height: 256)
        .ignoresSafeArea()
        .background(Color.blue)
        .onAppear {
          scene.isPaused = true
        }
        .onDisappear {
          scene.isPaused = false
        }
        
  }
  Button {
    withAnimation(.easeInOut(duration: 1.0)) {
      switcher.toggle()
    }
  } label: {
    Text("switch")
  }
}
}

class GameScene: SKScene {

static var shared = GameScene()

override func update(_ currentTime: TimeInterval) {
// Tells your app to perform any app-specific logic to update your scene.
  print("name ",scene!.name)
}
}