How can I stack SKScenes, and to use push/pop method to switch between the scenes?
Reason I'm asking is because i'm trying to implement an inventory management system to my rogue like game. I want the inventory to show up without transitioning out of the main games scene.
I tried to add an UI subview to the game scene but it didn't work out, because I couldn't add any images(anything that's a SKSpriteNode) to the subview. So, the only option I'm left with is to add another SKScene on top of the main game scene. (Unless someone can enlighten me as how to get SKSpriteNode to work within an UI subview)
Please show me a way to stack SKScenes, in Swift :)
Thanks in advance!
Related
I am writing an iOS app. It has a tab bar controller and one of the tab displays a view which has a collection view - it lists different mini games.
When I tap on the particular item, I show a view which consists of a both UIView and SKView. SKView presents the SKScene where most of the game play happens. There is a close button which closes this view and user is back to collection view.
Now everything works fine for first few iterations - Tap on a item in collection view, it shows a game and can play without hassle and close it. Animations are smooth. But if I just keep opening and closing this view, soon it becomes sluggish and animations are slower and slower. It also makes other parts of my app sluggish. I am clearing SKScene, deleting its children, also removing their actions. Instruments don't show any retail or memory leaks. Not sure whats going wrong. Please help!
Okay, answering my own question. NEVER EVER use NSTimer in SpriteKit. That was the problem in my case. Replacing it with update method fixed things for me.
I am working on a game with Sprite kit (swift) and I need a menu with buttons, stats, store etc.
I know I can make it with a SKScene but I want to edit it in the MainStoryBoard not only with code like in SKScene. If I create a new UIViewController file then just segue a button from that view to the SKScene will that be enough? Is it acceptable in terms of efficiency and memory management?
I think that will work fine for you, but it might be harder to work from a new UIViewController file than from SKScene. How effective creating the new UIViewController file versus creating the SKScene in code depends on how comfortable you feel in swift, so its hard to tell which you would prefer. I do not see any problems with memory management, but it could be less efficient than an SKScene because an SKScene was arguably designed for what you are trying to do. In summary, I believe it will be fine for you to use a new UIViewController, but an SKScene is likely the better route.
If you have a view controller in your storyboard (which you probably have if you go with apple's template), add a swift file like you do for any other app and connect it to your view controller.
Add any ui elements you want and add any actions and outlets.
Just make sure your SKScene is being shown IN your view controller. In most cases that is precoded.
SKScene can also be implemented in a UIView, so you can also drag a UIView in a view controller and set it as an SKScene file.
Hope it helps :)
As simple as this question may seem, I just couldn't figure it out. To the overflow community, I am using sprite kit to create a simple game, However when you lose the game I want to programmatically segue back to another viewController (basically the home screen). from The game scene I do not get access to "performSegueWithIdentifier" I've seen a couple answers that relate to this topic however non of them are in swift ... Any solutions?
links of related questions include : Link1
Thank You
I think you are misunderstanding SKViews. SKViews are just like a normal UIViews, they take place within a normal ViewController. You will therefore need to segue from the ViewController which is presenting your SKView.
I have the same issue in a game I have created (Ninja-Shooter). In my game, whenever I want to segue back the home screen I pass a value to NSUserDefaults.standardUserDefaults(). Then in my ViewController I am constantly listening for any change in that value, if the value does change, then I perform a segue.
Hope this helps!
I have discovered that using NSNotification Center Can in fact derive the results I was looking for. If any one in the future needs detailed code I will edit to provide that
When transitioning between SKScenes, is it possible to keep the first scene around and push the second scene on top of the first scene, so that it is possible to return to the first scene?
I.e. does something like pushviewcontroller exist for skscenes?
Yes, it's possible. You just need to have a strong reference to the currently disabled scene, for instance in a property of the view controller, to prevent it from deallocating and to be able to present it again when you "pop" the "pushed" scene.
See Kobold Kit's implementation of pushScene/popScene in its SKView subclass, which uses a regular array to hold on to the scenes on the "push stack": https://github.com/KoboldKit/KoboldKit/blob/master/KoboldKit/KoboldKitFree/Framework/View/KKView.m
I've just started playing around with SpriteKit, and this issue pretty much immediately cropped up. If I have a view controller whose view is an SKView, pushing another view controller on top of it (say, a pause menu or an end game menu) and then unwinding back to the view controller with the SKView takes a noticeable amount of time, in my experience on a 4S and a 5, about a second and a half.
You can test this using the default Hello World template. I set it as the root controller of a navigation view and stuck a button in the navigation bar to trigger a push segue to a new view controller. The second view controller contains a button which triggers an unwind segue. When the button gets pressed, it stays highlighted for about a second and a half, and then finally the segue happens, which is incredibly jarring to the user.
I've glanced through the SpriteKit documentation and didn't notice anything written about the proper use of segues, is this just a bug or is it considered bad practice to push new views on top of an SKView? Instead, should I be using SKNodes/SKScenes to present my pause and end game menus, thereby always keeping the SKView on screen?
I had a similar issue and found this helpful. I was planning on using multiple UIViewControllers to navigate to different sections of my game like a menu screen, game over screen etc. but it was painfully slow. After refactoring my app to use a single UIViewController whose view is of type SKView, and creating a new scene for each screen I wanted, it started to work properly. At first I thought that this would be a pain because the controller would end up getting unmanageable, but found that most of logic was incapsulated in each SKScene and the controller was used to present new scenes only.
Also, you should note that adding UIKit controls to the SKView from an SKScene is acceptable. This is what I am doing to avoid having to reinvent the wheel every time I need a UIScrollView, UIButton or any other UIKit control. Of course you will have to remove the UIKit stuff you don't want to share between scenes when you transition.
Could it be possible that the SKView (or the scene) deallocates while the menu storyboard is presented? That would explain the delay when returning to the scene. Set a breakpoint in the corresponding dealloc messages of custom view/scene subclasses or use Instruments to check for the view and scene being among the live objects while the menu is presented.
Another possibility could be textures being removed from memory and then having to be reloaded. There could be an automatism in Sprite Kit, hard to say.
Try changing the way your storyboards are connected, for example have a "main menu" storyboard before the sprite kit view. It would be interesting to see if you get the same effect entering the view (repeatedly) as you do going back to it.
FWIW I only did a few tests with SK and storyboards but haven't noticed such a delay.