Where do I place my code to instantiate my Project’s struct
code within SwiftUI
?
As you can see way below, I call:
_ = StaticObjects()
Within GameScene’s didMove
It seems the reason I don’t see the same #Preview
image in my GameScene
is that I am not explicitly returning a SwiftUI some View
within my struct
layout.
I do know that didMove
is really called .. but I just don’t see anything within the Simulator
My #Preview
works great:
The following code appears within GameScene.swift
of my Xcode Project:
import SpriteKit
import SwiftUI
struct StaticObjects : View {
// some View is returned when our struct is instantiated
var body: some View {
let theBuildings = ["hotel",
"saloon",
"watertower",
"jail",
"farmhouse",
"barn"]
let theFigures = ["desperado",
"sheriff",
"space",
"space",
"horse",
"guy"]
VStack {
HStack(alignment: .bottom) {
ForEach(theBuildings, id: \.self) { theBuilding in
Image(theBuilding)
.resizable() // for rotation
.scaledToFit()
.zIndex(2)
Spacer()
} // ForEach
} // HStack for theBuildings
HStack(alignment: .bottom) {
ForEach(theFigures, id: \.self) { theFigure in
Image(theFigure)
.resizable() // for rotation
.frame(width: 120, height: 230)
.offset(x: 0.0, y: -50.0)
.zIndex(2)
Spacer()
} // ForEach
} // HStack for theFigures
// aligns vertically the entire struct to the top
Spacer()
} // VStack
} // body:
} // struct StaticObjects
/*
#Preview {
StaticObjects()
}
*/
class GameScene: SKScene, SKPhysicsContactDelegate {
override func sceneDidLoad() {
super.sceneDidLoad()
} // sceneDidLoad
override func didMove(to view: SKView) {
super.didMove(to:view)
physicsWorld.contactDelegate = self
_ = StaticObjects() // doesn’t work
} // didMove
func didBegin(_ contact: SKPhysicsContact) {
// ...
} // didBegin
} // class GameScene