How to use SKPhysicsWorld without SKScene - ios

I am programming a command line server in Swift for my game which uses physics simulation from SKScene. I've built it as an OS X application using view controller but I couldn't figure out how to port it to command line. The problem I am running into now is the rendering loop of SKScene.
When I have a SKScene and I present it in an SKView, it runs normally like expected. But when I tried creating the scene and calling update on it manually when it's not presented inside an SKView, it doesn't update the its SKPhysicsWorld. It seems like the physics simulation only updates when you have the scene presented in an SKView.
Is there a work around for this? Is physics world of SKScene strictly tied to the UI? Is created a server using Swift a bad idea in general?

Related

Unresponsive Scene During Scene Change

I am working on a game with SpriteKit. I have finished my game, but wanted to add a main menu scene. I made two new files, "HomeScene.swift" and "HomeScene.sks". I was able to get the HomeScene to appear before the GameScene when I ran in the simulator, but it was not responsive to any touches, did not transition to the GameScene, or was affected by any code in my "HomeScene.swift" file.
Do I need to link the scene to the swift file somehow? Do I need to initialize the scene somehow?

Storyboard.hideOverlay: accessing parent functions issue

So Ive been wondering if this is possible to do, as it is important for my game.
Basically I have a menu.lua file (game plays only from this one file) but before the game loads up, I initiate this code which loads up a new lua file (tutorial.lua)
storyboard.showOverlay("tutorial", {effect="fromRight", isModal=true, time=50})
Now when I use this code to get back to menu.lua, the game does nothing.
storyboard.hideOverlay({"fromTop"})
The reason why is because my game begins from calling beginGame() from menu.lua. So I would like to ask if I can somehow call beginGame() from menu.lua once storyboard.hideOverlay code complete from within tutorial.lua
Cheers.
Corona no longer supports Storyboard implementation., So start using Corona Composer library. Corona Docs
In composer refer this to start the game when overlay hides. hideOverlay

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 transitioning scenes swift sprite kit

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 :)

cocos2dx animations lag whenever admob receives ad

I'm running smooth CCScaleTo actions in my cocos2dx(v2.0.4) game scene. After adding admob(v6.2.1) to cocos2dx's openGLView, the actions lag whenever admob receives a new ad.
I test two cases:
Remove the ad view from view hierarchy. The animations lag when admob loadRequst:
Comment out the admob loadRequest: and add the ad view back to view hierarchy. The animations lag within the first few seconds game scene load.
It seems related to UIKit & cocos2dx integration issue.
Any idea to workaround?
The trick that comes to mind is: don't let AdMob handle its own display (since it will insist on doing that animation). Instead, build the Adview, and only add it to your view hierarchy AFTER the normal animation would have finished, using i.e. dispatch_after.
Ideally you'd wrap the UIView in a cocos2d object so that CCDirector can do the animations instead of UIKit. That might be more work than it's worth, but it would definitely keep the animation thread from blocking.

Resources