Sorry for this being so long
From Hacking With Swift - 100 Days of Swift (UIKit edition)
I'm working on the first challenge for this project to track scores and I wanted to have a 'game over' feature when one of the players lost three lives. I thought I'd get a little fancy and use tiny gorillas for the number of lives left then hide them when one was lost. This basically works.
What doesn't work is that, when I present the screen after a life is lost, it's triggering didMove(to:) which, somehow, is resetting the arrays player1LivesNodes and player2LivesNodes.
This causes the createLives() method to recreate the two arrays from scratch. In the createLives() method, I do a test to see if both of the arrays are empty and only if they are empty do I create the nodes and add them to the arrays (for each player).
I have some properties set up to track number of lives per player. I keep arrays of each node created for each player in its own array.
And I have the didMove(to:) method which calls the function createLives() to populate the two arrays above.
In the createLives() function, I first test to see if both of the arrays of nodes for the players lives are empty or not. If they are empty, then I know I should create each node and populate the array for each player. If they are not empty, then I assume that I've already created the nodes and populated the arrays. Here's that function:
In the destroy(player:) function I check to see which player was destroyed, add to the score, subtract a life from the destroyed player, then set isHidden to true on the node from the appropriate array
Here's the output from running the program on a real iPad:
You can see that createLives() is called from didMove(to:) and that createLives() is creating the sprites and adding them to the playerLivesNodes arrays. You can't see this, but the sprites are being added to the screen properly. In destroy(player:) it properly destroys the player hit, adds 1 to the score, subtracts 1 from playerNumLives, hides the sprite node (and it is hidden from the screen). In the async code, a new game scene is being created, a transition is created, and the new scene is presented using the transition. Presenting the scene causes didMove(to:) to fire, which in turn calls createLives().
The problem is that, when didMove(to:) fires, the playerLivesNodes arrays are reset somehow and, because they're both empty, new nodes are created and added to the arrays. I can't figure out what or why this is happening.
I think, I now found the issue with this.
which creates new game scene.
You are able to track the score as you store vars in
And as soon as you destroy a player. You have new Scene created with 3 lives again, because you have hard coded it in
So solution might be to move your
SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.