Adding an Skscene to a Uiviewcontroller

Adding an SKScene to a UIViewController?

  1. Add an SKView to your viewController.
  2. Add an SKScene to your SKView.

You can overlay UIKit items from inside your SKScene. This is how I use collection and table views in my games. However they would need to be added as a subview on the SKView holding your SKScene.

SKScene has a view property that should hold a reference to the SKView its contained in. So it would just be a matter of doing something like this.

class MyScene: SKScene {
var someView: UIView

override func didMoveToView(view: SKView) {
someView = //setup view here
view.addSubview(someView)
}
}

Then inside this scene you can access your UIKit objects wherever you like.

Is it possible to use a SKScene with a UIView Controller?

Is it possible to use a SKScene with a UIView Controller?

Yes. You're probably doing it already without realizing it.

The bridge between SpriteKit and Cocoa Touch's view architecture is SKView. A lot of SpriteKit-based apps just have one view controller whose view is a SKView, and then they handle different phases of the game by doing all their drawing in SpriteKit and just changing sprite scenes. But you don't have to work that way -- you can mix SKView with other views and controls, and you can use as many view controllers as you like.

Here's a shot of a SKView inside another view and managed by a view controller that's embedded in a navigation controller:

Hello, world!

The purple area is a plain old UIView -- only the black area is a SpriteKit view.

Is there a way I can show a ball that uses CoreMotion to move without using SKScene?

Yes. If all you want to do is to move a ball across the screen, the UIKit animation built into UIView is almost certainly more than enough.

How To Present a SKScene from a UIViewController

I think I might have found a solution-I didn't present the GameScene directly from the MenuViewController, but instead, when the playButton is pressed I transitioned to the GameViewController and then presented the GameScene from there.

I don't know why it doesn't work in the MenuViewController, but this method works.

Getting the UIViewController from an SKScene

Your view controller is:

self.view?.window?.rootViewController

Example:

        let isReady = GADRewardBasedVideoAd.sharedInstance().isReady          
guard let controller = self.view?.window?.rootViewController as? GameViewController else {return}

if isReady {
print("ADMOB: started")
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: controller)
}

Segue in SKScene to UIViewController

You are calling the segue on the root viewController. I think that is the problem. You need to call the segue on the scene's viewController instead (where I am assuming you have created the segue, hence it is not being found on the root viewController).

Now the problem is that an SKScene does not have direct access to it's viewController, but just the view in which it is contained. You need to create a pointer to it manually. This can be done by creating a property for the SKScene:

class GameScene: SKScene {
weak var viewController: UIViewController?
...
}

Then, in the viewController class, just before skView.presentScene(scene)

scene.viewController = self

Now, you can access the viewController directly. Simply call the segue on this viewController:

func returnToMainMenu(){
viewController?.performSegueWithIdentifier("menu", sender: vc)
}

How do I present a UIViewController from SKScene?

You're creating a new view controller but never presenting it:

SpriteViewController *viewController = [SpriteViewController alloc];

I'm assuming that SpriteViewController is what presents your SpriteMyScene, and you'd like to hand control back to the presenting SpriteViewController.

You need to keep a reference to SpriteViewController in your SpriteMyScene subclass, and then access that reference when you call openTweetSheet.

in SpriteMyScene.h

@class SpriteViewController;

@interface SpriteMyScene : SKScene

@property (nonatomic, weak) SpriteViewController *spriteViewController;

@end

in SpriteViewController.m

// somewhere you initialize your SpriteMyScene object, I'm going to call it myScene

myScene.spriteViewController = self;

in SpriteMyScene.m

#import "SpriteViewController.h"

- (void)sendToController
{
NSLog(@"ok");
// use the already-created spriteViewController
[_spriteViewController openTweetSheet];
}


Related Topics



Leave a reply



Submit