我正在尝试在游戏场景中设置透明背景,以便当它在游戏视图中显示时会显示不同的背景。
这是我的游戏场景代码:
class Gamescene: SKScene, ObservableObject, SKPhysicsContactDelegate {
weak var gameDelegate: GamesceneDelegate?
// score functions
private var score: Int = 0
private var scoreLabel: SKLabelNode!
override init(size: CGSize) {
super.init(size: size)
self.backgroundColor = .clear // background
self.zPosition = -1
self.physicsWorld.contactDelegate = self // Setup physics contact delegate
print("Gamescene initialized with size: \(size)")
// Additional setup code as needed
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMove(to view: SKView) {
super.didMove(to: view)
view.allowsTransparency = true
backgroundColor = .clear //controls background
self.zPosition = -1
setupScene()
print("Gamescene did move to view")
}
private func setupScene() {
print("Gamescene setup complete")
}
这是我的游戏场景代码:
import SwiftUI
import SpriteKit
struct GameView: View {
@StateObject var scene: Gamescene
let backgroundNames = ["background1", "background2", "background3", "background4", "background5"]
@State private var offset: CGFloat = 0
let animationDuration: Double = 10.0 // Adjust animation duration as needed
init() {
let gameScene = Gamescene(size: CGSize(width: 400, height: 700))
gameScene.scaleMode = .fill
_scene = StateObject(wrappedValue: gameScene)
print("GameView initialized")
}
var body: some View {
ZStack {
// Sliding background
GeometryReader { geometry in
HStack(spacing: 0) {
ForEach(0..<(backgroundNames.count * 2), id: \.self) { index in
Image(backgroundNames[index % backgroundNames.count])
.resizable()
.frame(width: 1000, height: geometry.size.height)
.scaledToFill()
}
}
.frame(width: 1000 * CGFloat(backgroundNames.count * 2), height: geometry.size.height, alignment: .leading)
.offset(x: offset)
.onAppear {
startAnimation(totalWidth: 1000 * CGFloat(backgroundNames.count))
print("GameView appeared")
}
}
// SpriteView containing the game scene
SpriteView(scene: scene)
.frame(width: 400, height: 700)
.background(Color.clear)
.edgesIgnoringSafeArea(.all)
.onAppear {
if let skView = scene.view {
skView.allowsTransparency = true
print("SKView allows transparency: \(skView.allowsTransparency)")
}
}
}
}
func startAnimation(totalWidth: CGFloat) {
offset = 0
withAnimation(.linear(duration: animationDuration).repeatForever(autoreverses: false)) {
offset = -totalWidth
}
}
}
struct Gameview_Previews: PreviewProvider {
static var previews: some View {
GameView()
}
}
游戏场景背景应该清晰,以便在游戏视图中显示背景1,背景2,...。然而,即使设置为清除背景仍然显示为黑色
这里我将游戏场景更改为绿色背景
override func didMove(to view: SKView) {
super.didMove(to: view)
view.allowsTransparency = true
backgroundColor = .green //controls background
self.zPosition = -1
setupScene()
print("Gamescene did move to view")
}
并且背景在游戏视图中的背景1、背景2等之上显示为绿色,但是当我设置清除它时它只是黑色,我不知道为什么。我也尝试过改变 Z 位置,但这也没有帮助
对于透明背景
SKScene
,您应该做两件事。
将适当的选项添加到您的
SwiftUI
SpriteView
:
SpriteView(scene: scene, options: [.allowsTransparency])
在 SKScene 中将背景设置为
.clear
override func didMove(to view: SKView) {
self.backgroundColor = .clear
}
据我所知,在您的
view.allowsTransparency = true
上调用 SKView
似乎不会以某种方式影响结果。