What's the relationship between GameScene.swift and GameScene.sks files in SpriteKit template - swift

I've been playing around with SpriteKit, and am getting a pretty decent feel about how to drive it from code, but am pretty baffled by the level editor included in Xcode 6.
I've watched the wwdc videos ("platforms state of union" and "what's new in spriteKit"), and scrounged around the web, but haven't been able to find much description about the level editor, and what it's really doing.
What I don't understand, is how are the two files that the template sets up related? Is the .sks file an expression of the GameScene.swift file, or perhaps the GameScene class it contains?
OR do they both just hold separate objects/nodes that are going to play together in the same scene?
Or (this is my best explanation) is the .sks file and the editor basically for making the environment that responsive characters are going to "live" in? If that's true, how can my code in the .swift file relate to what's in the .sks file?

The .sks file is a static archive of your scene's content. If you've used Interface Builder to set up UI apps before, it's much the same idea, if rather different in implementation.
In a UI app, you could do everything in code:
override func viewDidLoad() {
let someText = UITextField(...)
let aButton = UIButton(...)
// ... position everything
// ... style everything
// ... etc ...
}
Or you could do all the static content setup in IB (a xib or storyboard), and use code only for setting up the dynamic behavior of your app — the things that happen when somebody starts touching those buttons. When you do that, the view controller you write code for exists as a proxy object in the xib/storyboard, making a bridge between what you set up in IB and what you set up in code.
In SpriteKit, you have the same choice. Before Xcode 6, many SK games took the all-code approach:
override func didMoveToView(view: SKView) {
let player = PlumberSprite(color: .Red)
player.position = // ...
player.physicsBody = // ...
self.addChild(player)
let ground = SKSpriteNode(...)
ground.position = // ...
ground.physicsBody = // ...
self.addChild(ground)
let block = QuestionBlockSprite()
block.position = // ...
block.physicsBody = // ...
block.contents = CoinSprite()
self.addChild(block)
// ... etc etc etc ...
}
That's a lot of code for what's ultimately a graphical, static scene — even before you start adding code to make it into a game (input handling, enemy behavior, physics callbacks that increment the score or go to game over, etc). And it doesn't lend itself well to designs where you split out the general game logic from the content, so it's harder to add multiple levels to your game.
Instead, you can use the SpriteKit editor in Xcode to build your static content (levels), and stick to code for dynamic behavior and game logic. Just like how, in IB, the view controller is a bridge between your storyboard and your code, your scene class (GameScene.swift in the template) is the bridge between the editor and code. When you load an .sks file at run time (the code for this is in GameViewController.swift in the template), it becomes an instance of your GameScene class, and anything you set up in the editor is accessible as child nodes of the scene.
Checking out WWDC talks is a good idea, but you missed the one that covers this: see session 608: Best Practices for Building SpriteKit Games for more on the motivation behind the SpriteKit editor, how to use it, and how to work with the scene contents loaded from an .sks file in your scene code.

Related

Creating new swift scene with corresponding sks file

So i've been playing around with swift for a few weeks now. I've messed around with objective C but i'm more of a java person so applied Objective C is a different ballgame to me. I can successfully create very basic games with simplistic transactions.
I created several scenes that i'm transitioning to which have nodes in them but since I didn't need create actions for those scenes (besides the default gamescene) I didn't even create any sks files for them.
So now i'm finally jumping into creating actions for nodes in a specific swift file and so I created a sks file with the same name as the swift file (I know there's more to it than that in order to link the two together). My question is, how do I link the sks file to the swift file so I can then apply actions and so I can go about and start manipulating nodes programmatically?
I have a scene going to a scene which should theoretically allow me to do certain things if certain nodes are pressed but I can't seem to get anything done in the swift file to correspond to the actual sks file like i've done in the gamescene file or the way I did with view controllers.
Thanks for any help :D!
P.S: I used this guide: https://www.youtube.com/watch?v=7wJu7f9mStg
This can be done via initializing a new action.. check SKAction documentation (or autocomplete in xcode) to see it's initializers.
Here is a snippet based on Apple's website that will do what you need:
// init?(named: String)
// Creates an action of the given name from an action file.
let action = SKAction(named: "whateverYourActionnameIS")
Scroll all the way to the bottom to see the list of functions, inits, etc, for SKAction:
https://developer.apple.com/reference/spritekit/skaction

Loading a new SKScene for the first time lags

I'm making a game a game in Xcode 6 using SpriteKit and swift. When I transfer from my starting scene to my "Play Game Scene" for the first time it takes 5 to 10 seconds. I have tried going to this scene from various other scenes and I still get this same lag. I thought it was the code at first, but this problem only recently started, but also, after I go to this scene the first time (and it lags), then I transfer to a different scene (the game over scene), when I go back to it, it no longer lags. It only lags the first time I transfer to this scene. I have also tried making a new project and copying and pasting all of the code from all of the scenes to a new scenes. If there is a simple explanation for this, please help. I don't understand why it would only lag the first time I go to the scene. If there isn't a simple answer, I suppose I will have to read through all of the code...AGAIN. Any help is appreciated. Thanks!
-Callum
Your scene is probably loading textures or other files into the memory the first time you start the scene, and then these remain in the memory for the rest of the session. If you have texture atlases that you are loading in the scene, you can pre-load these using:
SKTextureAtlas.preloadTextureAtlases(textureAtlases: [AnyObject]!, withCompletionHandler completionHandler: (() -> Void)!)
8 months later I came back to the project and found out it was because I use a non-specific font name. The font I used was "Felt Marker", but it should have been "Felt Market-Thin". If you don't give an exact font name, it takes a few seconds to figure out it was not specific so it goes to the default.
Here is a list of all the IOS fonts with their specific types:
http://iosfonts.com/

Basic iPhone app development issue regarding basic UI

I have a basic question regarding iOS development. I have created view file ViewController.xib. I do have ViewController.h file. The xib file contains components, such as text field, stepper and so on. When I run the iPhone app the window is empty. I know that I need to drag and drop components from xib file to ViewController.h file in order to make properties, but this is what I cannot achieve. I drag the items them self but there supposed to be a pointer arrow being dragged up.
As far as I can recall I need to perform some other things even before this operation by dragging files somewhere. So, can someone tell me what needs to be done, please.
Best regards
If your .xib is loading properly, then any controls in the view should be visible, they just won't function if they aren't 'wired' into your class. It sounds like you're just setting out on iPhone development, I would recommend you look at Ray Wenderlich's Storyboard tutorial - this takes you through creating a basic app, and creating the view hierarchy: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
Basically .xib file is a design template.Which is associated with a viewcontroller.h and viewcontroller.m files. You can place controls by dragging from the objects section from the right pane.These controls will be created automatically at runtime.To address these controls you need to make connection between them and the control variables you declare in your viewcontroller.h file.Even if you are not making connections , the controls will be visible and functional but u cannot control them, because you dont have it connected to an object reference

Smoke (or) explosion animation while removing an item from my iPhone App

I want to remove an item (lets say a UIButton) from my iPhone application. I want to add some animation when i tap on the button to remove it.
This is the animation i want:
In your OSX dock, if you right-click on an item and tap on *remove from dock*,
it kinda like **explodes** with a funny noise, and removes itself from that dock.
Any knows how to do that smoke (or) tiny explosion animation on the iPhone ?? Is there a pre-defined name for it ?
Believe it or not, there's an API for exactly that on Mac OS X:
NSPoint centrePoint = ...;
NSSize size = ...;
NSShowAnimationEffect(NSAnimationEffectPoof, centrePoint, size, nil, NULL, NULL);
On iOS, there isn't because the animation is a Mac-specific animation. On iOS, one typically sees the deleted object collapse into a point. That animation can be done by animating the transform of a view (using CAAnimation or the UIView class methods) so that it scales to nothingness.
You can roll your own using CAEmitterLayer - there are lots of examples on SO and elsewhere. You could do some really nice stuff (or maybe find some really nice open source code).

Preloading assets in cocos2D

I have some assets for my game that are being loaded when the gameplay layer is loaded. I'd prefer to load them when the application launches in a loading screen, which is obviously a pretty standard thing to do.
My problem is the way in which new scenes are launched within cocos2D. Consider the following code which occurs (with some variations) in several places throughout the project:
[[CCDirector sharedDirector] replaceScene:[CCTransitionShrinkGrow transitionWithDuration:0.5f scene:[GameplayLayer scene]]];
This is the standard way of replacing the current scene with a new one. My question is, given that format, how would I pass a preloaded asset to the new GameplayLayer? Is there an accepted way of doing this in cocos2D? I have a feeling that I'm missing something incredibly simple, but as of now it's a mystery to me.
Cocos2d uses a texture cache that persists between scenes. You can preload assets into this cache in a loading scene and they will still be available from your game scene. How you load these are up to you. For images you can opt to do it asynchronously, to allow your loading scene to maintain a decent framerate and render a progress bar.
This post gets the basic idea across.
This thread may also be of use to you in that regard: http://www.cocos2d-iphone.org/forum/topic/2242
If you have your own class of assets (i.e. not a supported cocos2d image, or something), you could create your own cache singleton class (look at how the sharedManager instances of various cocos2d classes are implemented) and load your assets into that. As far as memory management goes you'd have to release those assets yourself whenever you deem necessary, but that's rather beyond the scope of this question.