SKScene file rendered by SpriteView shows empty grey background/box?

74 views Asked by At

I'm trying to render my SKScene file using SpriteView(). Instead of displaying the scene, it just displays an empty grey background. I tried this with two different SKScene files, and they both were rendered as a grey background.

However, if I define and initialise a new SKScene completely in code (i.e. not using a SKScene file and the visual editor), and render it using SpriteView(), it works fine.

What could I be doing wrong?


Here is an example of my test code:

This is the code for rendering SKScene file. Result: Grey screen. Note GameScene is an SKScene file, with accompanying swift file.

import SwiftUI
import SpriteKit

var scene: SKScene {
    let scene = GameScene()
    scene.scaleMode = .aspectFill
    return scene
}

struct ContentView: View {
    var body: some View {
        
        SpriteView(scene: scene)
            .ignoresSafeArea()
    }
}

//  to view canvas
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Code for creating a SKScene completely in code. Result: displays scene properly, and interactions work.

import SwiftUI
import SpriteKit

class CodeScene: 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)
    }
}

// A sample SwiftUI creating a GameScene and sizing it
// at 300x400 points
struct ContentView: View {
    var scene: SKScene {
        let scene = CodeScene()
        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()
    }
}

    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }

Edit: Added Game Scene swift file:

import SpriteKit

class GameScene: SKScene {
    
    private var Player: SKSpriteNode!
    
    override func didMove(to view: SKView) {
        Player = childNode(withName: "//Player") as? SKSpriteNode
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        guard let touch = touches.first else { return }
        let touchLocation = touch.location(in: self)
        let distance = sqrt(pow(touchLocation.x - Player.position.x, 2) + pow(touchLocation.y - Player.position.y, 2))
        let speed: CGFloat = 120.0 // adjust as needed
        let duration = TimeInterval(distance / speed)
        let moveAction = SKAction.move(to: touchLocation, duration: duration)
        Player.run(moveAction)
    }
    
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}
0

There are 0 answers