Sprite kit 与SwiftUI一起使用时如何调整SpriteKit SKScene的大小
我正在尝试将SpriteKit与SwiftUI结合使用。但是当我在Swiftui中使用它时,我在调整游戏场景的大小方面遇到了困难 这是我目前的编码 首先,游戏场景的编码:Sprite kit 与SwiftUI一起使用时如何调整SpriteKit SKScene的大小,sprite-kit,swiftui,Sprite Kit,Swiftui,我正在尝试将SpriteKit与SwiftUI结合使用。但是当我在Swiftui中使用它时,我在调整游戏场景的大小方面遇到了困难 这是我目前的编码 首先,游戏场景的编码: import SpriteKit import GameplayKit @objcMembers class GameScene: SKScene { let player = SKSpriteNode(imageNamed: "player-motorbike") var touch
import SpriteKit
import GameplayKit
@objcMembers
class GameScene: SKScene {
let player = SKSpriteNode(imageNamed: "player-motorbike")
var touchingPlayer = false
override func didMove(to view: SKView) {
view.allowsTransparency = true
self.backgroundColor = .clear
if let particles = SKEmitterNode(fileNamed: "Mud") {
let farRightPt = frame.maxX // start the emitter at the far right x-point of the view
particles.advanceSimulationTime(10)
particles.position.x = farRightPt
addChild(particles)
}
let nearLeftPt = frame.minX * 3 / 4 // start the player at a quarter of the way to the far left x-point of the view
player.position.x = nearLeftPt
player.zPosition = 1
addChild(player)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user touches the screen
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let tappedNodes = nodes(at: location)
if tappedNodes.contains(player) {
touchingPlayer = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touchingPlayer else { return }
guard let touch = touches.first else { return }
let location = touch.location(in: self)
player.position = location
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// this method is called when the user stops touching the screen
touchingPlayer = false
}
override func update(_ currentTime: TimeInterval) {
// this method is called before each frame is rendered
}
}
上面的编码将游戏场景向下推,使其仅占据屏幕的三分之一左右
我尝试了其他方法来调整游戏场景的大小。我最幸运的是设置:
scene.size = CGSize(width: 300, height: 400)
这允许游戏场景填满整个屏幕,但对象显示为扭曲
我想我应该在这里使用GeometryReader,但是因为场景不是某个视图,所以也不起作用
有人知道如何调整SKScene的大小,以便在SwiftUI中使用吗?我也有类似的问题。你偶然解决了吗?
scene.size = CGSize(width: 300, height: 400)