I have a game in which the main menu presents the game scene, and when the player loses, the game scene presents back the menu. When transitioning from the menu to the game scene, the menu's deinit is successfully called. My problem is that the opposite doesn't happen (when transitioning from the game scene to the menu scene, gameScene's deinit isn't called).
I suspect a strong reference cycle directly between the new main menu instance that I create and the game scene which creates the instance. Matter of facts, if I edit my gameScene's code as following, I do get the deinit's call, but my app crashes at runtime while trying to present the scene:
class GameScene: SKScene {
// Some stuff
// Now the function which is called when I want to present back the menu scene:
func lose() {
// Some stuff
// Delay is a helper function
delay(bySeconds: 2.0, closure: { [unowned self] in
for child in self.children {
child.removeFromParent()
}
unowned let menu = MainMenu(size: CGSize(width: 1152, height: 2048))
let reveal = SKTransition.fade(with: SKColor.black, duration: 1.0)
self.view?.presentScene(menu, transition: reveal)
})
}
}
At runtime I get the following error:
Attempted to retain deallocated object
Here's the definition of delay:
public func delay(bySeconds seconds: Double, dispatchLevel: DispatchLevel = .main, closure: #escaping () -> Void) {
let dispatchTime = DispatchTime.now() + seconds
dispatchLevel.dispatchQueue.asyncAfter(deadline: dispatchTime, execute: closure)
}
public enum DispatchLevel {
case main, userInteractive, userInitiated, utility, background
var dispatchQueue: DispatchQueue {
switch self {
case .main: return DispatchQueue.main
case .userInteractive: return DispatchQueue.global(qos: .userInteractive)
case .userInitiated: return DispatchQueue.global(qos: .userInitiated)
case .utility: return DispatchQueue.global(qos: .utility)
case .background: return DispatchQueue.global(qos: .background)
}
}
}
And here's my MainMenu class:
class MainMenu: SKScene {
let gameScene = GameScene(size: CGSize(width: 1152, height: 2048))
let reveal = SKTransition.fade(with: SKColor.black, duration: 1.0)
// Some stuff
// override func didMove(to ...
// Now the function which is called when I want to present the game scene:
func presentGame() {
view?.presentScene(gameScene, transition: reveal)
}
}
I've tried different things but they didn't work out (for example: setting the new menu instance in the init method of gameScene).
If I remove the "unowned" definition of the menu constant in the GameScene class, the transition is successfully made but deinit isn't called.
Any suggestions?
I finally found what had been keeping my game scene from deinitializing itself.
It had nothing to do with the code that I provided with my question. The problem was in a strong "self" reference inside a closure which was responsible to interpret the accelerometer's input.
If you're interested in more informations, see here: Retain cycle suspected in closure
Related
I'm trying to learn how to make a GameManager type class, and making individual classes for each of my GameScenes... probably the wrong thing to do, but for the sake of this question, please accept this as the way to do things.
My GameManager looks like this, having a reference to each of the scenes, that's static:
import SpriteKit
class GM {
static let scene2 = SecondScene()
static let scene3 = ThirdScene()
static let home = SKScene(fileNamed: "GameScene")
}
How do I create a SKScene programmatically, without size info, since they're in a subclass of SKScene and don't have any idea what the view size is, and I don't want them to need worry about this:
I'm doing this, but getting a EXC_BAD_Access at convenience override init()
class SecondScene: SKScene {
override init(size: CGSize){
super.init(size: size)
}
convenience override init(){
self.init()
self.backgroundColor = SKColor.red
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
}
}
As I mentioned your question is a bit vague but lets do some examples of what a GameManager class can be.
Before I start lets differentiate between calling this
let scene = StartScene(size: ...)
and this
let scene = SKScene(fileNamed: "StartScene")
The 1st method, with size, is when you create your scenes all in code and you are not using the xCode visual level editor.
The 2nd method is when you are using the Xcode level editor, so you would need to create a StartScene.sks file. Its that .sks file that it looks for in fileNamed.
Now for some game manager example, lets first imagine we have 3 SKScenes.
class StartScene: SKScene {
override func didMove(to view: SKView) { ... }
}
class GameScene: SKScene {
override func didMove(to view: SKView) { ... }
}
class GameOverScene: SKScene {
override func didMove(to view: SKView) { ... }
}
Lets say you want to transition from StartScene to GameScene, you would add this code in your StartScene at the correct spot e.g when the play button is pressed. Thats the simplest way to move from one SKScene to the next, directly from the SKScene itself.
// Code only, no xCode level editor
let gameScene = GameScene(size: CGSize(...))
let transition = SKTransition...
gameScene.scaleMode = .aspectFill
view?.presentScene(gameScene, transition: transition)
// With xCode level editor (returns an optional so needs if let
// This will need the GameScene.sks file with the correct custom class set up in the inspector
// Returns optional
if let gameScene = SKScene(fileNamed: "GameScene") {
let transition = SKTransition...
gameScene.scaleMode = .aspectFill
view?.presentScene(gameScene, transition: transition)
}
Now for some actual examples of GameManagers, Im sure you know about some of them already.
EXAMPLE 1
Lets say we want a scene loading manager. You approach with static methods will not work because a new instance of SKScene needs be created when you transition to one, otherwise stuff like enemies etc will not reset. Your approach with static methods means you would use the same instance every time and that is no good.
I personally use a protocol extension for this.
Create a new .swift file and call it SceneLoaderManager or something and add this code
enum SceneIdentifier: String {
case start = "StartScene"
case game = "GameScene"
case gameOver = "GameOverScene"
}
private let sceneSize = CGSize(width: ..., height: ...)
protocol SceneManager { }
extension SceneManager where Self: SKScene {
// No xCode level editor
func loadScene(withIdentifier identifier: SceneIdentifier) {
let scene: SKScene
switch identifier {
case .start:
scene = StartScene(size: sceneSize)
case .game:
scene = GameScene(size: sceneSize)
case .gameOver:
scene = GameOverScene(size: sceneSize)
}
let transition = SKTransition...\
scene.scaleMode = .aspectFill
view?.presentScene(scene, transition: transition)
}
// With xCode level editor
func loadScene(withIdentifier identifier: SceneIdentifier) {
guard let scene = SKScene(fileNamed: identifier.rawValue) else { return }
scene.scaleMode = .aspectFill
let transition = SKTransition...
view?.presentScene(scene, transition: transition)
}
}
Now in the 3 scenes conform to the protocol
class StartScene: SKScene, SceneManager { ... }
and call the load method like so, using 1 of the 3 enum cases as the scene identifier.
loadScene(withIdentifier: .game)
EXAMPLE 2
Lets make a game manager class for game data using the Singleton approach.
class GameData {
static let shared = GameData()
private init() { } // Private singleton init
var highscore = 0
func updateHighscore(forScore score: Int) {
guard score > highscore else { return }
highscore = score
save()
}
func save() {
// Some code to save the highscore property e.g UserDefaults or by archiving the whole GameData class
}
}
Now anywhere in your project you can say
GameData.shared.updateHighscore(forScore: SOMESCORE)
You tend to use Singleton for things where you only need 1 instance of the class. A good usage example for Singleton classes would be things such as helper classes for Game Center, InAppPurchases, GameData etc
EXAMPLE 3
Generic helper for storing some values you might need across all scenes. This uses static method approach similar to what you were trying to do. I like to use this for things such as game settings, to have them in a nice centralised spot.
class GameHelper {
static let enemySpawnTime: TimeInterval = 5
static let enemyBossHealth = 5
static let playerSpeed = ...
}
Use them like so in your scenes
... = GameHelper.playerSpeed
EXAMPLE 4
A class to manage SKSpriteNodes e.g enemies
class Enemy: SKSpriteNode {
var health = 5
init(imageNamed: String) {
let texture = SKTexture(imageNamed: imageNamed)
super.init(texture: texture, color: SKColor.clear, size: texture.size())
}
func reduceHealth(by amount: Int) {
health -= amount
}
}
Than in your scene you can create enemies using this helper class and call the methods and properties on it. This way you can add 10 enemies easily and individually manage their health etc. e.g
let enemy1 = Enemy(imageNamed: "Enemy1")
let enemy2 = Enemy(imageNamed: "Enemy2")
enemy1.reduceHealth(by: 3)
enemy2.reduceHealth(by: 1)
Its a massive answer but I hope this helps.
Desired behavior is: when an action is removed from a node (with removeAction(forKey:) for instance) it stops to animate and all the changes caused by action are discarded, so the node returns back to pervious state. In other words, I want to achieve behavior similar to CAAnimation.
But when a SKAction is removed, the node remains changed. It's not good, because to restore it's state I need to know exactly what action was removed. And if I then change the action, I also will need to update the node state restoration.
Update:
The particular purpose is to show possible move in a match-3 game. When I show a move, pieces start pulsating (scale action, repeating forever). And when the user moves I want to stop showing the move, so I remove the action. As the result, pieces may remain downscaled. Later I would like to add more fancy and complicated animations, so I want to be able to edit it easily.
Thanks to the helpful comment and answer I came to my own solution. I think the state machine would be bit too heavy here. Instead I created a wrapper node, which main purpose is run the animation. It also has a state: isAimating property. But, first of all, it allows to keep startAnimating() and stopAnimating() methods close to each other, incapsulated, so it's more difficult to mess up.
class ShowMoveAnimNode: SKNode {
let animKey = "showMove"
var isAnimating: Bool = false {
didSet {
guard oldValue != isAnimating else { return }
if isAnimating {
startAnimating()
} else {
stopAnimating()
}
}
}
private func startAnimating() {
let shortPeriod = 0.2
let scaleDown = SKAction.scale(by: 0.75, duration: shortPeriod)
let seq = SKAction.sequence([scaleDown,
scaleDown.reversed(),
scaleDown,
scaleDown.reversed(),
SKAction.wait(forDuration: shortPeriod * 6)])
let repeated = SKAction.repeatForever(seq)
run(repeated, withKey: animKey)
}
private func stopAnimating() {
removeAction(forKey: animKey)
xScale = 1
yScale = 1
}
}
Usage: just add everything that should be animated to this node. Works well with simple animations, like: fade, scale and move.
As #Knight0fDragon suggested, you would be better off using the GKStateMachine functionality, I will give you an example.
First declare the states of your player/character in your scene
lazy var playerState: GKStateMachine = GKStateMachine(states: [
Idle(scene: self),
Run(scene: self)
])
Then you need to create a class for each of these states, in this example I will show you only the Idle class
import SpriteKit
import GameplayKit
class Idle: GKState {
weak var scene: GameScene?
init(scene: SKScene) {
self.scene = scene as? GameScene
super.init()
}
override func didEnter(from previousState: GKState?) {
//Here you can make changes to your character when it enters this state, for example, change his texture.
}
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass is Run.Type //This is pretty obvious by the method name, which states can the character go to from this state.
}
override func update(deltaTime seconds: TimeInterval) {
//Here is the update method for this state, lets say you have a button which controls your character velocity, then you can check if the player go over a certain velocity you make it go to the Run state.
if playerVelocity > 500 { //playerVelocity is just an example of a variable to check the player velocity.
scene?.playerState.enter(Run.self)
}
}
}
Now of course in your scene you need to do two things, first is initialize the character to a certain state or else it will remain stateless, so you can to this in the didMove method.
override func didMove(to view: SKView) {
playerState.enter(Idle.self)
}
And last but no least is make sure the scene update method calls the state update method.
override func update(_ currentTime: TimeInterval) {
playerState.update(deltaTime: currentTime)
}
I have a function in my ScoreSystem class named addScore. The function adds 1 point to the game, updates the SKLabelNode to the current score and in turn calls the function startNewLevel every 25 points.
func addScore(scene: SKScene) {
gameScore += 1
scoreLabel.text = "\(gameScore)"
if CGFloat(gameScore).truncatingRemainder(dividingBy: 25) == 0 {
NotificationCenter.default.post(name: Notification.Name.init("start_new_level"), object: nil)
GameScreen().displayLevel(scene: scene)
}
}
The function gets called every time a torpedo that has been fired hits the enemy. I now want to add a new level where meteors (SKSpriteNode) have to be avoided. I have several SKActions in a sequence to accomplish this. Essentially, the SKSpriteNode moves from the top of the screen, reaches below the screen and gets deleted. If the meteor reaches the bottom of the screen means that it has been avoided by the player.
I'm attempting to call the function addScore but it doesn't update.
Here is the function:
let scoreSystem = ScoreSystem()
func spawnMeteor() {
let randomXStart = CGFloat.random(min: gameArea.minX, max: gameArea.maxX)
let startPoint = CGPoint(x: randomXStart, y: scene.size.height * 1.2)
let endPoint = CGPoint(x: randomXStart, y: -scene.size.height * 0.2)
let meteor = SKSpriteNode(imageNamed: "meteor")
meteor.name = "Meteor"
meteor.zPosition = 2
meteor.position = startPoint
let moveMeteor = SKAction.move(to: endPoint, duration: 3)
let deleteEnemy = SKAction.removeFromParent()
let score = SKAction.run(addToScore)
let meteorSequence = SKAction.sequence([
moveMeteor,
score,
deleteEnemy])
scene.addChild(meteor)
meteor.run(meteorSequence)
}
I have tried a function addToScore like this:
func addToScore() {
scoreSystem.addScore(scene: scene!)
}
And also tried this
func addToScore() {
NotificationCenter.default.post(name: Notification.Name.init("add_to_score"), object: nil)
}
When trying this second alternative, I add the following to the GameScene
override func sceneDidLoad() {
super.sceneDidLoad()
NotificationCenter.default.addObserver(forName: Notification.Name.init("add_to_score"), object: nil, queue: OperationQueue.main) { [weak self] (notification) in
self?.scoreSystem.addScore(scene: self!)
}
}
I removed several lines from the spawnMeteor() function so not to clog the space with unnecessary lines of code. I have yet to figure out how to call that function using SKAction.run(). Can someone point me in the right direction?
You are passing along a lot of information to your functions, probably too much.
I would suggest either you implement Protocols or Notifications to handle your information, personally I prefer protocols so my example will be protocols.
I am making some assumptions about your code because not all of it is presented in your question...
protocol ScoreSystemDelegate: class {
func displayLevel()
}
class ScoreSystem: SKNode {
weak var scoreSystemDelegate: ScoreSystemDelegate!
//your init func and any other funcs in this class (unknown)
func addScore(scene: SKScene) {
gameScore += 1
scoreLabel.text = "\(gameScore)"
if CGFloat(gameScore).truncatingRemainder(dividingBy: 25) == 0 {
NotificationCenter.default.post(name: Notification.Name.init("start_new_level"), object: nil)
//GameScreen().displayLevel(scene: scene)
self.scoreSystemDelegate.displayLevel()
}
}
}
in your class that creates the scoreSystem (assuming GameScene)...
class GameScene: SKScene, ScoreSystemDelegate {
let scoreSystem = ScoreSystem()
//assign GameScene as the delegate of ScoreSystem that way the 2 can communicate
scoreSystem.scoreSystemDelegate = self
func displayLevel() {
//do whatever you do when you display the level
}
}
now your spawn meteor func should work as you have coded because addScore no longer takes a scene property, nice thing about this approach is that you can make any object a delegate of ScoreSystem it doesn't have to be a scene.
Hello guys i have made the first Level of my game, but always when i go from the main menu screen to the first level the screen freezes for like 2 Seconds and the transition from the Main screen to the game is very delayed and laggy and it sometimes doesn't even show up. Is there a way to preload the Scene in the background to prevent the lag?
you can load the resources for the scene in a different thread. I do this in my game to get really snappy scene transitions despite the fact im loading tons of resources.
make a static function in your scene class to preload your scene
class func createResources(withCompletion: (scene: BaseScene) -> ()){
// load resources on other thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
let scene = YourScene()
// callback on main thread
dispatch_async(dispatch_get_main_queue(), {
// Call the completion handler back on the main queue.
withCompletion(scene: scene)
});
})
}
call it like this
YourScene.createResources(withCompletion: {
[weak self]
scene in
self!.skView.presentScene(scene)
})
So the way to use this is to build your scene in advance on the different thread. since its running on a different thread you shouldnt get that awkward pause.
for example. lets say the player reaches the goal of beating the level. before I was using this method the game would pause for a second before loading the next scene.
When the player beats the level now I still allow them to move around until the next scene has loaded and then the player will instantly shoot into the next level creating an instant transition.
you can see it here when the ship is hyperspacing between levels. there are a lot of resources loading but the transitions are seamless.
https://www.youtube.com/watch?v=u_bXA3woOmo
Swift 5 version of #hamobi answer
file: DispatchQueueExtensions.swift
import Foundation
extension DispatchQueue {
static func background(_ task: #escaping () -> Void) {
DispatchQueue.global(qos: .background).async {
task()
}
}
static func main(_ task: #escaping () -> Void) {
DispatchQueue.main.async {
task()
}
}
}
file: GameScene.swift
extension GameScene {
class func create(completion: #escaping (_ scene: GameScene) -> Void) {
DispatchQueue.background {
let scene = GameScene()
DispatchQueue.main {
completion(scene)
}
}
}
}
Usage:
GameScene.create(completion: { [weak self] scene in
let transition = SKTransition.doorway(withDuration: 1.0)
self?.view?.presentScene(scene, transition: transition)
})
I am writing a game using SpriteKit with Swift and have run into a memory concern.
The layout of my game is such that the GameViewController (UIViewController) presents the first SKScene (levelChooserScene) in the viewDidLoad Screen. This scene does nothing more than display a bunch of buttons. When the user selects a button the scene then transitions to the correct scene using skView.presentScene, and when the level is complete, that scene then transitions back to the levelChooserScene and the game is ready for the user to select the next level.
The problem is that when the transition back to the levelChooserScene occurs the memory allocated for the game play scene is not deallocated, so after selecting only a few levels I start receiving memory errors.
Is my design correct in transitioning from SKScene to SKScene, or should I instead return to the GameViewController each time and then transition to the next SKScene from there?
I have found a few posts on here that say I should call skView.presentScene(nil) between scenes, but I am confused on how or where to implement that.
I simply want to transition from one SKScene to another and have the memory used from the outgoing scene to be returned to the system.
This is an example of how I have implemented the SKScene:
class Level3: SKScene
{
var explodingRockTimer = NSTimer()
var blowingUpTheRocks = SKAction()
override func didMoveToView(view: SKView)
{
NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "dismissTheScene:", userInfo: nil, repeats: false)
var wait = SKAction.waitForDuration(0.5)
var run = SKAction.runBlock{
// your code here ...
self.explodeSomeRocks()
}
let runIt = SKAction.sequence([wait,run])
self.runAction(SKAction.repeatActionForever(runIt), withKey: "blowingUpRocks")
var dismissalWait = SKAction.waitForDuration(5.0)
var dismissalRun = SKAction.runBlock{
self.removeActionForKey("blowingUpRocks")
self.dismissTheScene()
}
self.runAction(SKAction.sequence([dismissalWait,dismissalRun]))
}
func explodeSomeRocks()
{
println("Timer fired")
}
//MARK: - Dismiss back to the level selector
func dismissTheScene()
{
let skView = self.view as SKView?
var nextScene = SKScene()
nextScene = LevelChooserScene()
nextScene.size = skView!.bounds.size
nextScene.scaleMode = .AspectFill
var sceneTransition = SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1.5) //WithDuration(2.0)
//var sceneTransition = SKTransition.pushWithDirection(SKTransitionDirection.Down, duration: 0.75) //WithDuration(2.0)
//var sceneTransition = SKTransition.crossFadeWithDuration(1.0)
//var sceneTransition = SKTransition.doorwayWithDuration(1.0)
sceneTransition.pausesOutgoingScene = true
skView!.presentScene(nextScene, transition: sceneTransition)
}
}
Well the thing that was causing my trouble was inserting particle emitters every half second for 5 seconds using SKAction.repeatActionForever() to call the emitter insert function.
This repeatAction apparently was not killed by transitioning to another scene, and was causing the memory for the whole scene to be retained. I switched to SKAction.repeatAction() instead and specify how many time it should fire. The scene now returns all of its memory when I transition to the new scene.
I am not sure I understand this behavior though.
SpriteKit it's not strongly documented when it comes to create complex games. I personally had a problem like this for days until I managed to figure it out.
Some objects retain the reference, so it doesn't deinit. (SKActions, Timers, etc)
Before presenting a new scene I call a prepare_deinit() function where I manually remove the strong references which are usually not deallocated by swift.
func prepare_deinit()
{
game_timer.invalidate() // for Timer()
removeAction(forKey: "blowingUpRocks") // for SKAction in your case
// I usually add the specific actions to an object and then remove
object.removeAllActions()
// If you create your own object/class that doesn't deinit, remove all object
//actions and the object itself
custom_object.removeAllActions()
custom_object.removeFromParent()
}
deinit
{
print("GameScene deinited")
}
The last problem I encountered was that the new scene was presented much faster than my prepare_deinit() so I had to present the new scene a little later, giving the prepare_deinit() enough time to deallocate all objects.
let new_scene =
{
let transition = SKTransition.flipVertical(withDuration: 1.0)
let next_scene = FinishScene(fileNamed: "FinishScene")
next_scene?.scaleMode = self.scaleMode
next_scene?.name = "finish"
self.view?.presentScene(next_scene!, transition: transition)
}
run(SKAction.sequence([SKAction.run(prepare_deinit), SKAction.wait(forDuration: 0.25), SKAction.run(exit_to_finish)]))