Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating a SpriteKit view into a xib view

I have a view that I already created using a xib file.

Now I would like to add some small elements to this view that would make use some of the physics animations from SpriteKit, so now I need an SKView.

Is it possible to add an SKView as a Subview of the view that corresponds to my xib view? I tried this and it does not seem to show anything.

The following is in the ViewController corresponding to my XIB view:

this.myCustomSKView = new CustomSKView()
this.View.AddSubview( this.myCustomSKView );

and the ViewController for my custom SKView has:

    public override void ViewWillLayoutSubviews ()
    {
        base.ViewWillLayoutSubviews ();
        if(this.SKView.Scene == null)
        {
            this.SKView.ShowsFPS = true;
            this.SKView.ShowsNodeCount = true;
            this.SKView.ShowsDrawCount = true;

            var scene = new MyCustomSKScene (this.SKView.Bounds.Size);
            this.SKView.PresentScene (scene);
        }
    }
like image 819
L. Desjardins Avatar asked Oct 20 '22 23:10

L. Desjardins


1 Answers

I just gave this a try, and worked.

  1. Started with single view app.
  2. Dragged in myScene.m / .h from another sprite kit app into my project.
  3. In storyboard dragged in a UIView, and set class to SKView in storyboard.
  4. Created an outlet from that to the VC class (in my case called it myGame)
  5. Added the #import in the VC class
  6. Also copied from the demo project viewDidLoad

This is the only change i made

-(void)viewDidLoad {
   [super viewDidLoad];
   // Next line is all I changed...
   SKView * skView = (SKView *)self.view;
   skView.showsFPS = YES;
   skView.showsNodeCount = YES;

   // Create and configure the scene.
   SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
   scene.scaleMode = SKSceneScaleModeAspectFill;

   // Present the scene.
   [skView presentScene:scene];
}

I added in some other UIKit to kind show how its a little SK Game in a view.

enter image description here

Not sure if this is the best way but I hope I answers your question.

I agree with lionserdar, and you should check out UIKit Dynamics instead.

like image 156
DogCoffee Avatar answered Oct 24 '22 01:10

DogCoffee