Post

Replies

Boosts

Views

Activity

Reply to How to add a .sks file to a playground?
It's possible! I did it in mine. You'll have to add your .sks in the PrivateResourses folder and you'll have to call it in ChaptersChapter1 (or inside the chapter you want)PagesPlaygroundPage1(or inside the page you want)main: swift /*:  Text  Text!  */ //#-hidden-code import PlaygroundSupport import SpriteKit import BookCore // Load the SKScene from 'GameScene.sks' let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 683, height: 1024)) // I called my .sks controller Main1 and the fileNamed is the name of .sks if let scene = Main1(fileNamed: "Main1") {     // Set the scale mode     scene.scaleMode = .aspectFit     // Present the scene     sceneView.presentScene(scene) } PlaygroundSupport.PlaygroundPage.current.liveView = sceneView //#-end-hidden-code Hope it helps!
Apr ’21
Reply to How to transition from a SwiftUI Page to a SpriteKit Scene?
I don't know how you do this for playgrounds, but that's how you integrate spritekit with swiftUI, you use a spriteView: swift // A simple game scene with falling boxes class GameScene: SKScene { override func didMove(to view: SKView) { physicsBody = SKPhysicsBody(edgeLoopFrom: frame) } override func touchesBegan(_ touches: SetUITouch, with event: UIEvent?) { guard let touch = touches.first else { return } let location = touch.location(in: self) let box = SKSpriteNode(color: SKColor.red, size: CGSize(width: 50, height: 50)) box.position = location box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50)) addChild(box) } } // A sample SwiftUI creating a GameScene and sizing it // at 300x400 points struct ContentView: View { var scene: SKScene { let scene = GameScene() scene.size = CGSize(width: 300, height: 400) scene.scaleMode = .fill return scene } var body: some View { SpriteView(scene: scene) .frame(width: 300, height: 400) .ignoresSafeArea() } } Hope it helps :)
Apr ’21
Reply to How can I use SpriteKit with SwiftUI?
This is how i solved it: ContentView.swift import SwiftUI struct ContentView: View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;SpriteKitContainer(scene: SpriteScene()) &#9;&#9;} } struct ContentView_Previews: PreviewProvider { &#9;&#9;static var previews: some View { &#9;&#9;&#9;&#9;ContentView() &#9;&#9;} } SpriteKitContainer.swift import SwiftUI import SpriteKit struct SpriteKitContainer: UIViewRepresentable { &#9;&#9;typealias UIViewType = SKView &#9;&#9; &#9;&#9;var skScene: SKScene! &#9;&#9; &#9;&#9;init(scene: SKScene) { &#9;&#9;&#9;&#9;skScene = scene &#9;&#9;&#9;&#9;self.skScene.scaleMode = .resizeFill &#9;&#9;} &#9;&#9; &#9;&#9;class Coordinator: NSObject { &#9;&#9;&#9;&#9;var scene: SKScene? &#9;&#9;} &#9;&#9; &#9;&#9;func makeCoordinator() -> Coordinator { &#9;&#9;&#9;&#9;let coordinator = Coordinator() &#9;&#9;&#9;&#9;coordinator.scene = self.skScene &#9;&#9;&#9;&#9;return coordinator &#9;&#9;} &#9;&#9; &#9;&#9;func makeUIView(context: Context) -> SKView { &#9;&#9;&#9;&#9;let view = SKView(frame: .zero) &#9;&#9;&#9;&#9;view.preferredFramesPerSecond = 60 &#9;&#9;&#9;&#9;view.showsFPS = true &#9;&#9;&#9;&#9;view.showsNodeCount = true &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;return view &#9;&#9;} &#9;&#9; &#9;&#9;func updateUIView(_ view: SKView, context: Context) { &#9;&#9;&#9;&#9;view.presentScene(context.coordinator.scene) &#9;&#9;} } struct SpriteKitContainer_Previews: PreviewProvider { &#9;&#9;static var previews: some View { &#9;&#9;&#9;&#9;Text("Hello, World!") &#9;&#9;} } SpriteKitScene.swift import UIKit import SpriteKit class SpriteScene: SKScene { &#9;&#9; &#9;&#9;//change the code below to whatever you want to happen on skscene &#9;&#9; &#9;&#9;override func didMove(to view: SKView) { &#9;&#9;&#9;&#9;physicsBody = SKPhysicsBody(edgeLoopFrom: frame) &#9;&#9;} &#9;&#9; &#9;&#9;override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { &#9;&#9;&#9;&#9;guard let touch = touches.first else { return } &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;let location = touch.location(in: self) &#9;&#9;&#9;&#9;let box = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50)) &#9;&#9;&#9;&#9;box.position = location &#9;&#9;&#9;&#9;box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50)) &#9;&#9;&#9;&#9;addChild(box) &#9;&#9;} } PS: you'll only see the spritekitscene working in the simulator, it won't work in the preview
Sep ’20
Reply to SwiftUI and Spritekit integration
It is possible. You'll need to create a SpriteKitContainer.swift: import SwiftUI import SpriteKit struct SpriteKitContainer: UIViewRepresentable {     typealias UIViewType = SKView          var skScene: SKScene!          init(scene: SKScene) {         skScene = scene         self.skScene.scaleMode = .resizeFill     }          class Coordinator: NSObject {         var scene: SKScene?     }          func makeCoordinator() -> Coordinator {         let coordinator = Coordinator()         coordinator.scene = self.skScene         return coordinator     }          func makeUIView(context: Context) -> SKView {         let view = SKView(frame: .zero)         view.preferredFramesPerSecond = 60         view.showsFPS = true         view.showsNodeCount = true                  return view     }          func updateUIView(_ view: SKView, context: Context) {         view.presentScene(context.coordinator.scene)     } } struct SpriteKitContainer_Previews: PreviewProvider {     static var previews: some View {         /*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/     } } After that, you need to create your skscene that will be something like this: import UIKit import SpriteKit class SpriteScene: SKScene {          /*change the code below to whatever you want to happen on skscene*/          override func didMove(to view: SKView) {         physicsBody = SKPhysicsBody(edgeLoopFrom: frame)     }          override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {         guard let touch = touches.first else { return }                  let location = touch.location(in: self)         let box = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))         box.position = location         box.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 50, height: 50))         addChild(box)     } } And this is how you will call the spritekitscene in your ContentView: import SwiftUI struct ContentView: View {     var body: some View {         SpriteKitContainer(scene: SpriteScene())     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } } PS: you'll only see the spritekitscene working in the simulator, it won't work in the preview
Sep ’20