TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How can I prevent didMove(to view: SKView) from firing twice when scene is called by SpriteView()?

Forums > SwiftUI

I have a GameScene called from the body of a SwiftUI view. In iOS 15, everything works perfectly. However, in earlier versions of iOS, it does not work. Specifically, as print statements indicate, didMove(toView: SKView) is being called twice. This causes a "start" button reappear and the desired game actions to run invisibly in the background. Below is the SwiftUI view that contains the SpriteView().
I think the issue is being caused by calls to the viewModel in the game scene which are used to update the SwiftUI view in which the SpriteView() appears. Is there a way to circumvent this? I suspect there is, because it works in iOS 15 (but not earlier versions). Please let me know if I have gone about this all wrong. Thanks!

struct NyonindoChallengeView: View {
    @ObservedObject var viewModel = NyonindoChallengeViewModel()
    var gameScene: SKScene {
        let scene = NyonindoGameScene(
            size: CGSize(
                width: UIScreen.main.bounds.width,
                height: UIScreen.main.bounds.height
            ),
            attempts: viewModel.attempts
        )
        scene.viewModel = self.viewModel
        scene.scaleMode = .aspectFill
        return scene
        }

    var endScene: SKScene {
        let scene = NyonindoSummaryScene(size: CGSize(
                                            width: UIScreen.main.bounds.width,
                                            height: UIScreen.main.bounds.height
            )
        )
        scene.viewModel = self.viewModel
        scene.scaleMode = .aspectFill
        return scene
        }

var body: some View {
    ZStack {
        GeometryReader { geometry in
            if viewModel.gameOver {
                SpriteView(scene: endScene,
                           transition: .crossFade(
                            withDuration: 1.0
                           )
                )
                    .frame(
                        width: geometry.size.width,
                        height: geometry.size.height
                    )
            } else {
                SpriteView(scene: gameScene,
                           transition: .crossFade(
                            withDuration: 1.0
                           )
                )
                    .frame(
                        width: geometry.size.width,
                        height: geometry.size.height
                    )
                }
        }
        .edgesIgnoringSafeArea(.all)
        .navigationBarTitle(Text(""))
        .navigationBarHidden(true)
        .statusBar(hidden: true)
        VStack {
            HStack {
                Text(viewModel.gameActive ? "Round \(viewModel.attempts + 1) of 3" : "")
                    .font(.subheadline)
                    .bold()
                    .padding()
                Spacer()
                Text("Try to beat \(viewModel.highestScore)")
                    .font(.subheadline)
                    .bold()
                    .foregroundColor(.black)
                    .padding()
            }
            Spacer()
        }
        .edgesIgnoringSafeArea([.top])

            if viewModel.gameOver {
                let earnedPoints = viewModel.highestScore
                let points = min(nyonindoChallenge.maxPoints, earnedPoints)
                ChallengeFeedbackView(challenge: nyonindoChallenge, points: points, success: true, text: "Well done!")
            }
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.