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)
}
}
Post
Replies
Boosts
Views
Activity
When I set up a dragGesture using a gestureState, the gestureState has the correct value during the onChange phase. But when the onEnded phase is entered, the gesturePhase is reset to its original default value.
Is this the correct behavior?
I would like the gestureState to retain its value since I want to make use of it in the onEnded phase. After the onEnded phase is completed, then the gestureState should be reset to its original value.