I'm trying to add a SpriteKit scene to my view using
SpriteView(scene:SKScene(fileNamed: "SceneName")!)
. This works well, but how can I interact with the scene?
Thanks in advance for your answer ❤️
I'm trying to add a SpriteKit scene to my view using
SpriteView(scene:SKScene(fileNamed: "SceneName")!)
. This works well, but how can I interact with the scene?
Thanks in advance for your answer ❤️
Create a subclass of SKScene
for your SpriteKit scene and use that class to write the code to interact with the scene.
Interact with the scene by overriding the touch functions on iOS and the mouse functions on Mac. On iOS you must override touchesBegan
. You may also need to override touchesMoved
and touchesEnded
. On Mac you must override mouseDown
. You may also need to override mouseDragged
and mouseUp
.
The code I gave you is a little wrong because I wrote it on the go, sorry about that. So, the code from the project:
//imports...
struct MapView: View {
var body: some View {
ZStack {
SpriteView(scene:MapScene())
}
}
}
class MapScene: SKScene {
class func newGameScene() -> MapScene {
guard let scene = SKScene(fileNamed: "MapScene") as? MapScene else {
print("Failed to load GameScene.sks")
abort()
}
scene.scaleMode = .aspectFill
return scene
}
}
Changes applied, but it still shows a black screen.
It shows a black screen because you passed an empty scene to the sprite view.
SpriteView(scene:MapScene())
Calling MapScene()
creates an empty scene.
You must add a property to your view that holds the scene to show in the sprite view. Call newGameScene
to initialize it.
@State var mapScene = MapScene.newGameScene()
I haven't tested that line of code so you may have to make some changes to get it to work. But it gives you an idea of what you need to do.
Pass that property to the sprite view.
SpriteView(scene: mapScene)
It works with some changes. But I still can't interact with the MapScene
because the SpriteView
now calls the newGameScene
function rather than the MapScene
class.
I have not tried loading a SpriteKit scene from a .sks file to a SwiftUI sprite view so I don't have a specific solution for you. I have some tips and suggestions that may help you solve the problem yourself or give other people here enough information to help you.
It works with some changes
Show the code you use to declare the property for the scene in the SwiftUI view.
I still can't interact with the MapScene
Provide more details. What are you trying to do? What do you expect to happen? What happens?
Set a breakpoint in your touchesBegan
or mouseDown
functions. If you run your project and tap or click in the scene, does the breakpoint fire? If you have not used Xcode's debugger before, read the following article:
https://www.swiftdevjournal.com/an-introduction-to-xcodes-debugger/
I doubt you are the first person to have trouble loading a SpriteKit scene into a SwiftUI view. Search on Stack Overflow or a search engine, and chances are high someone has had the same problem and received a solution.
Show the code you use to declare the property for the scene in the SwiftUI view
Here it is:
private var mainCamera: SKCameraNode?
class func newGameScene() -> SKScene {
if let scene = SKScene(fileNamed: "MapScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
return scene
} else {
abort()
}
}
}
What are you trying to do?
I'm trying to access to interact with my scene from a .sks file. By interact, I mean writing code to interact with the scene, like we do when we have a SpriteKit project.
What do you expect to happen?
I can program my scene.
What happens?
I can see my scene in the built project, but I can't program it.
But even if you couldn't help me, at least you tried. So thank you ❤️