如何解决带有 SpriteView 和 SwiftUI 的场景不会填满整个屏幕
我正在尝试处理项目 1 并使用 SpriteKit 和 SwiftUI,但由于某种原因,场景从未填满我 iPad 上的整个屏幕。
import SwiftUI
import SpriteKit
struct ContentView: View {
var scene: SKScene {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let scene = GameScene()
scene.size = CGSize(width: screenHeight,height: screenWidth)
scene.scaleMode = .fill
scene.backgroundColor = .green
return scene
}
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
print(screenWidth)
return SpriteView(scene: scene)
.frame(width: screenWidth,height: screenHeight)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
现在看起来像这样:
知道我在这里缺少什么吗?
最大
解决方法
最后通过将背景放在 ContentView 的 ZStack() 中修复了它
var body: some View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
ZStack{
EmptyView()
Image("road")
.resizable()
.aspectRatio(contentMode: .fill)
.ignoresSafeArea()
SpriteView(scene: scene,options: [.allowsTransparency])
.frame(width: screenWidth,height: screenHeight)
.ignoresSafeArea()
}
}
}
出于某种原因,当我将其添加为 GameScene 类的一部分时,无论我尝试什么比例,它都不会将纹理放大到全屏大小:
let background = SKSpriteNode(imageNamed: "road")
background.zPosition = -1
// background.scale(to: CGSize(width: 1180,height: 700))
// background.scale(to: CGSize(width: CGFloat(1180),height: CGFloat(820)))
addChild(background)
,
您在场景属性中有 height at width 和 width at height 参数。 (@aheze 如何在评论中提到)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。