The focus here is selecting the proper .scaleMode
.
Please note that the graphics will be greatly enhanced in the next couple of weeks. But in the meantime, I thought I'd concentrate on the coding.
===== EDIT ======
I have decided to focus on using HStack
by adding it to my GameScene
code with this:
Note that my struct
is just defined here .. it is instantiated via _ = StaticObjects()
within func didMove(..)
As explained further down, this declaration is at the very top of class GameScene
. The following declaration just defines it as a var within our class GameScene: SKScene
What is a big mystery to me if how the HStack is actually drawn? It's placed within the class GameScene', but somehow I do not put in the code required to draw it within my
GameScene`.
class GameScene: SKScene, SKPhysicsContactDelegate {
struct StaticObjects: View {
var body: some View {
#if os(iOS)
let imageFolder = "images_iOS/"
#elseif os(tvOS)
let imageFolder = "images_tvOS/"
#endif
let theBuildings: [String] = [imageFolder + "hotel",
imageFolder + "saloon",
imageFolder + "watertower",
imageFolder + "jail",
imageFolder + "farmhouse",
imageFolder + "barn"]
let theFigures: [String] = [imageFolder + "desperado",
imageFolder + "sheriff",
imageFolder + "horse",
imageFolder + "guy"]
HStack(alignment: .bottom) {
// “Initializer 'init(_:rowContent:)' requires that
// ‘SomeType’ conform to 'Identifiable’”
//
// Using a List or ForEach on primitive types that don’t
// conform to the Identifiable protocol, such as an array
// of strings or integers.
ForEach(theBuildings, id: \.self) { theBuilding in
Image(theBuilding)
.resizable()
.scaledToFit()
.zIndex(2)
Spacer()
} // ForEach
} // HStack
HStack(alignment: .bottom) {
ForEach(theFigures, id: \.self) { theFigure in
Image(theFigure)
.resizable()
.scaledToFit()
.offset(x: 0.0, y: -15.0)
.zIndex(2)
Spacer()
} // ForEach
} // HStack
} // body:
} // struct
override func sceneDidLoad() {
super.sceneDidLoad()
} // sceneDidLoad
override func didMove(to view: SKView) {
super.didMove(to:view)
physicsWorld.contactDelegate = self
_ = StaticObjects() // instantiate my struct
} // didMove
func didBegin(_ contact: SKPhysicsContact) {
} // didBegin
} // class GameScene
Unfortunately, I must still be doing something that is basically wrong.
For example, am I placing this struct
in the right place (= GameScene
?)
===== end EDIT =====
Using .resizeFill
shows this for portrait view:
Using .resizeFill
shows this for landscape view:
Please note that for the landscape view, the line of SKSpriteNodes
(and the railroad tracks) got greatly inset which is unsatisfactory. In the portrait view, this line + tracks are spread out edge-to-edge horizontally which is what I'd like to happen also in the landscape view
I then tried .aspectFill
. This .scaleMode
did eliminate the above mentioned inset which is great. But, as expected, in each case, portrait + landscape, the top-most Status Light (SKShapeNode
) got clipped.
So, how do I get the best of both worlds = no clipping of the Status Light and no inset of the SKSpriteNodes
??
Please note that the line of SKSpriteNodes
are graphically added to the GameScene's .sks
file. The railroad tracks, the train + dog are dynamically added with Swift coding because the train + dog move along the tracks and these tracks are the UIBezierPath
along which the train + dog move.