App crashes transitioning scenes swift sprite kit - ios

I am building an app that has a main scene and a restart scene. The main game scene uses sprite kit physics to have an object controlled by the user float across the screen. When it rashes into an obstacle the app transitions to the end scene using:
self.presentScene.restartscene.
This works just fine. then the restart scene has a restart button. when this is clicked the app transitions back to the game scene using:
self.presentscene.gamescene
i know this presents the original scene because i did an ns log to make sure. The problem is when this original scene is presented again the scene changes colors multiple times alternates very quickly between one and two nodes on the screen and then crashes. I have no idea the cause. i have had this problem before and i know other people have. no one has given me a definite answer. i hope one of you guys will. Thank you. help needed badly!!!!!!!

Try to navigate between scenes this way:
let nextScene = GameScene(fileNamed: "SceneNameHere")!
scene?.view?.presentScene(nextScene)
Also please check your code on startup in the gamescene.
May be you are using some global flags to store value for win/fail/restart conditions and your scenes simply open together one by one (it is hard to say without checking actual code).

You need to remove the original seen from view after transitioning away from it. Use this code:
myView.removeFromParentViewController()
Hope this helps :)

Related

How do I freeze an AR scene in ARKit and then restore the scene elsewhere - Swift, XCode

I have an AR scene (built as a node with children nodes) that I want the user to walk inside it.
Because it can be large, I added a pause button in the corner so that if the user hits an obstacle in the real world, he'll just need to press-and-hold that button to freeze the scene, move elsewhere in the real world, and when the button will be released the scene would have moved as-is to that new location, retaining the same relative position and rotation inside that scene.
What I did is very simple. When button is touched down I just did:
sceneView.session.pause()
And when the button is Touch Up Inside I ran:
sceneView.session.run(configuration)
This seemed to do the trick.
However, after I clicked and moved once or twice more and moved back to where I was before, my whole AR scene suddenly jumped back to its previous location in the real world. So it did freeze and showed up in the new location for a second or two, but then it jumped back.
I tried to resume the session with several of the available options.
If I ran:
sceneView.session.run(configuration, options: [.resetTracking])
the whole scene would move to the new location, but it would be set to its initial position and rotation as if I was standing at (0,0,0) when I started the app, but now moved to the new location. In other words, it lost the current location and rotation that I was at when I pressed the pause button and just reset the scene.
All the other options that I tried had the same effect as if I didn't add any options.
I should add that I'm not using any anchors (so far) nor any ray casting.
I didn't try different combinations of those options (except removeExistingAnchors and resetTracking together), but I doubt that that would help. Am I wrong?
What am I doing wrong?
Anyone have a solution for my problem?
Or will I need to manually record the current position and rotation when pressing the pause button and then restore those with some position change of the node? I was hoping I could skip having to do it this way.
And if I do need to do it manually, any tips on how to achieve this?
Thanks for your help!

Architecture of a SceneKit game, multiple view controllers for menu screens?

I'm working on a SceneKit game using Swift, and I'm thinking about the best way to organize the various screens/ states surrounding the actual main game scene. I have a simple prototype which consists of three states:
Splash Screen (choose game type) ->
Game ->
Game over screen (choose to play again or return to splash)
In the prototype I opted to go for UIKit to do the UI elements, rather than a SpriteKit overlay. Although it's just buttons at the moment, which would be easy enough to do in SpriteKit, I might in future want to add sliders (eg an options screen to set control sensitivity etc), text entry etc, and then you end up recreating great chunks of uikit.
So at present, each of the 3 scenes described above has its own UIViewController. The Splash and Game ViewControllers have their own SCNViews and SCNScenes, and the GameOver is a modal that displays over the main Game scene.
This structure isn't really ideal, as it means that the main game SCNScene gets reloaded whenever the viewDidLoad of the GameViewController fires. The main SCNScene is quite detailed so takes a few seconds to load, and with repeated cycles this seems to create memory issues. After 2 cycles of Splash -> Game -> GameOver I get a memory warning, and after the third cycle Xcode loses the connection to the iPhone (which seems, in my experience, to be caused by memory issues).
I would like to have a main GameViewController that loads the most frequently reused assets once, but still be able to segue between the various menu screens.
So what are the options here?
I thought perhaps I could have a main GameViewController which controls the loading and unloading of SceneKit assets and has the sole SCNView. Its viewDidLoad method would only be fired once, when the app first starts. Then, each of the other views would have a very minimal UIViewController, which would be presented as modals over the main GameViewController, with "OverCurrentContext" selected.
I wanted to ask whether this pattern sounds feasible, and how others organise their SceneKit games. I'm still quite new to native iOS development, so it could be that I'm missing some basic game design pattern.
My Experience with the use of only one GameViewController (my current work for MAC OS X: i started a a 'small' 3D game with a MainMenu/start screen, a Hud and 2 or 3 complete different 3D Scenes - this shall be ported then to IOS, too):
In the last week i tried what you asked for, to use only one GameViewController for "all" that stuff - for me it seemed to be the easy and "good" way to do it - but even after lot of hours with all my tricks I've learned the last years i was not able to reload load another (or change) 3D scene (or even not another sprite kit scene hud) after a scenekit scene is loaded in a single ViewController.
Maybe there is difference between MAC OS X and IOS but i even tried this in an iOS version and it has the same behavior.
What i was able to do: You can modify a loaded 3D scene or change data in the sprite kit hud etc., so you are able to use one 3d scene to add or show the things up when they are needed but as far as my tests here showed, you will need one ViewController to show up a complete new different 3d scene or 2d/3d Menus etc. I will post my further experiences as soon as I'm a step further. i hope this helps a little.

app crashes when transitioning to original scene swift sprite kit

I am building an app that transitions to an end scene when two object collide. That part works just fine, but when i try to transition back to the original scene with the restart button on the end scene.The app crashes. It makes it back to the original scene but crashes immediately.The the line that is selected in the debugger is where i declare the physics body of one of the objects that collide(works fine first run. I have seen this error many times. But no one can give me an answer of how to fix it. Here is the code where i transition from the original seen to the end scene
func mineSubCrash(Mine : SKSpriteNode, subm: SKSpriteNode){
subm.removeFromParent()
Mine.removeFromParent()
self.view?.presentScene(RestartScene())
}
very basic, here is the transition back
func restart(){
restartButton.removeFromSuperview()
self.view?.presentScene(GameScene(), transition: SKTransition.fadeWithDuration(0.3))
}
I dont understand why it crashes. must be something with me not ending the original scene right . I have had this problem with other apps i have tried to make. help needed and appreciated!!

Problems going back and forth between scenes (Sprite Kit)

I am new to the iOS platform and have written an app using Sprite Kit and Xamarin. To transition between scenes I am using NavigationController.PushViewController with a "Back" button on the new scene to return. This works fine.
My problem occurs when I go back and forth between the original scene and next scene about 3 times. The frame rate (and overall application) slows to a crawl, and depending on which scenes, will hard crash. This happens only on device and not in the simulator. My gut instinct says I'm leaking memory somewhere / not cleaning up properly, so I have just started getting up-to-speed with Instruments. Note that each scene has about 5-8 images on it.
Questions:
What is the proper way to clean-up after transitioning out of a scene back to the previous one? I am explicitly calling RemoveAllActions() and RemoveAllChildren() to ensure clean-up.
What is my best strategy when using Instruments? I've only just begun to experiment with an Allocations trace, but there are a lot of allocations and knowing what to look for is challenging.
Any help is appreciated. I am considering dumping Sprite Kit altogether since my app doesn't really need it (I wanted to experiment) and because it has been a challenge building the app with all kinds of deep crashes (e.g. SKShapeNode, which is very problematic).
Thanks.

Gamecenter init/authentication makes SpriteKit's sprite movement lagging

I have a simple scenario (default SpriteKit template): one ViewController and a MyScene SpriteKit scene.
In my ViewController I managed in viewDidLoad the gamecenter initialization but my scene after start immediatelly starts a rather quick sprite movement infinitely (a background parallax scrolling).
Everything works correctly but until GameCenter goes through the authentication procedure my sprite movement lags, breaks, not running fluently.
What practice can I have to minimize this (with keeping the parallax sprite movement running)?
I'm testing this on iPhone4 device (IOS7.03)
GameCenter authentication is actually something that has been causing lag in games for awhile. There are a couple ways people try to get around it, and it's really depends on the app and programmers taste.
Pause the game during GameCenter authentication
This is the easiest way typically, and pretty straightforward.
Put the authentication on another thread
If you desire a fluid option from menu to gameplay, this would be the route to go.
Just use GCD to start a new thread and run the GameCenter authentication in it.
Hope this helps. There might be a couple of other ways, but these are the two I've seen the most. I typically just use the first method.

Resources