How to load files in memory for better scene transition?
Picking up from the comments, there are multiple ways to break up loading. Here is one that popped in my head:
class Scened: SKScene {
var needsLoading = false
func loadChunk1() {}
func loadChunk2() {}
func loadChunk3() {}
func loadChunk4() {}
func loadChunk5() {}
override func didMove(to view: SKView) {
if needsLoading { loadChunk1() }
}
override func didEvaluateActions() {
if needsLoading { loadChunk2() }
}
override func didSimulatePhysics() {
if needsLoading { loadChunk3() }
}
override func didApplyConstraints() {
if needsLoading { loadChunk4() }
}
override func didFinishUpdate() {
if needsLoading {
loadChunk5()
// Disable loading after the last chunk:
needsLoading = false
}
}
}
What's the best way to handle multiple SKScenes?
Having a Single View Controller and Multiple Scenes
You could use single view controller (which is actually a default state of SpriteKit game template) and have multiple scenes.
So you will have GameViewController
and LandingScene
, GameScene
and possible some other scenes, like LevelSelect
scene or something like that.
In GameViewController
, you initialize your scene for the first time. So that is the point where you initialize your LandingScene
(I guess that is the place where you implemented your navigation menu).
So, from that point, you are able to make a transition from any scene you want using SKView's
presentScene: method (and optionally using SKTransition
class).
Transitioning
Transition in SpriteKit
can be generally done in two different ways:
1. Some might tell you that making a transition from a current scene, to the next scene is a "bad design" and that the current scene should notify the view controller about its ready-to-transition state, so that view controller can make needed transition. But in response to that, these are quotes from docs:
Transitioning Between Two Scenes
Typically, you transition to a new scene based on gameplay or user
input. For example, if the user presses a button in your main menu
scene, you might transition to a new scene to configure the match the
player wants to play.
And the related code:
- (void)mouseUp:(NSEvent *)theEvent
{
[self runAction: self.buttonPressAnimation];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
GameConfigScene *newScene = [[GameConfigScene alloc] initWithSize: CGSizeMake(1024,768)]];
[self.scene.view presentScene: newScene transition: reveal];
}
It can be clearly seen that transition to the next scene is done within current scene.
2. Using the method described above using delegation pattern, where scene delegates responsibility of transitioning to the view controller.
Both ways are perfectly fine, where the first method is a bit convenient IMO, and widely used. So that is how you can navigate between different scenes in SpriteKit
in an easy way.
Hint:
Don't forget to override scene's dealloc
(or deinit
if you use Swift) method while in development phase, to make sure that all scenes are deallocated correctly.
How to create transition into SKAction?
You can do it like this:
override func didMoveToView(view: SKView) {
colorize()
}
func colorize(){
let colorize = SKAction.sequence([
SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3),
SKAction.runBlock({[unowned self] in self.colorize()})
])
runAction(colorize, withKey: "colorizing")
}
This is recursive function which calls itself every time colorizeWithColor action is finished. This is required due to fact that just repeating this:
SKAction.colorizeWithColor(UIColor.randomColor(), colorBlendFactor: 1, duration: 3)
inside an action sequence will colorize the background always to the same color. That will happen because when you create an action once, you can't change it over time (you can change its speed or pause it for example, but you can't change a duration
or any other passed parameter).Instead, we re-create the action associated with a certain key each time. And this is from the docs about actions associated with keys:
If an action using the same key is already running, it is removed
before the new action is added.
So each time we run a new action, associated with "colorizing" key, the previous action is removed and there will be always only one action with that key.
Related Topics
Longer Subtitles in Mapview Annotations (Swift)
Updating the Status Bar Style Between View Controllers
Wkwebview Decidepolicyfornavigationaction Being Call After the Long Press Recogniser Ended
Which Passes How to Access in Apple Wallet
Generate a Hmac Swift Sdk8.3 Using Cchmac()
iOS Swift Remove Uitableview Cell Separator Space
How to Send Request from iOS (Swift) to Dialogflow V2 API Without Cloud Functions
Swift: How to Change Detailview Depending on Sidebar Selection State with Swiftui 4
How to Position UI Elements on Background Image Relative to the Image
Your Credentials Do Not Allow Access to This Resource Twitter API Error
Please Add the Host Targets for the Embedded Targets to the Podfile
How to Download and View Images from the New Firebase Storage
Square Video Using Avfoundation
How to Add the Same Background Image to All Views in My iOS App Using Swift
Cannot Access Appdelegate While Testing Xcode Project