I am using Xcode 14.2 and running on Ventura 13.2.
There is no problem working with iOS or the OSX playground.
But there is a problem having it work with OSX.
The first problem is that the DragGesture is not recognized on OSX: nothing happens on a drag of an SKNode.
The second problem (using a kludge by putting a "glass pane" in front of the spriteView) is that the dragging now works, but the coordinate system is flipped.
Here is the code:
import SwiftUI
import SpriteKit
@main
struct TestOnMacApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var gameScene = GameScene()
let glassPaneColor =
CGColor(red: 0, green: 0, blue: 0, alpha: 0.01)
var body: some View {
ZStack {
SpriteView(scene: gameScene)
// Comment out the following line: drag won't work
Color(cgColor: glassPaneColor)
}
.gesture(DragGesture(minimumDistance: 0)
.onChanged { gesture in
let location = gesture.location
let sceneLocation =
gameScene.convertPoint(fromView: location)
gameScene.spriteNode.position = sceneLocation
})
}
}
class GameScene: SKScene {
let sceneSize = CGSize(width: 500, height: 500)
let nodeSize = CGSize(width: 50, height: 50)
let spriteNode: SKSpriteNode
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init() {
spriteNode = SKSpriteNode(texture: nil, size: nodeSize)
super.init(size: sceneSize)
scaleMode = .resizeFill
backgroundColor = .white
spriteNode.name = "spriteNode"
spriteNode.color = .blue
spriteNode.position = CGPoint(x: 100, y: 100)
addChild(spriteNode)
}
}