7

I've been searching and searching but can't seem to find a solution that works for what I'm trying to do, and I'm almost at the point where I have to ask if it is even possible.

I'm using Xamarin Studio to develop an iOS app. I have a few different screens set up as UIViewControllers and they are working well. The crux of the app, however, is a game and I want to use Monogame as I've used it before and enjoyed working with it.

Basically, I have to switch from a UIViewController to the Game class. I am able to do this by simply creating a new Game object and calling Run(), but I can not figure out how to get out of the Game when I need to and return control back to the UIViewController.

Has anybody done this in an app? I've tried everything I can possibly think of, but nothing seems to do the trick. Is it even possible? Or will I need to redo it so that Monogame handles everything, even all of the other stuff in the app that isn't part of the actual game?

Thanks!

halterdev
  • 333
  • 4
  • 17
  • I don't know exactly what you're trying to do but have you considered injecting an interface into your game class? – craftworkgames May 05 '14 at 05:16
  • 1
    I have a TabController. One of the games is a "Game" tab. Once that tab is clicked, there are a few options. One of which is a button, "New Game", that I want to fire up a Game object. Once the game is over, I want to return to the Game tab that the user came from, and end the Game object. – halterdev May 05 '14 at 13:00

1 Answers1

5

There are a couple hacky ways I feel this will work without modifying MonoGame.

1st option:

  1. When your app starts, start your Game class as you would a normal MonoGame game
  2. To display a controller present it modally over top of the game
  3. To return to the game, dismiss your modal controller

So for example, to show a controller:

var gameController = game.Services.GetService(typeof(UIViewController)) as UIViewController;
gameController.PresentViewController(new YourController(), true, null);

Then you can just call DismissViewController to hide it. I've used this for implementing Game Center stuff.

Sadly, you will have to modify how MonoGame works if you want to show your MonoGame game after a UIKit controller.

Another option is to basically add a UIView on top of your MonoGame's view. Something like this:

var gameController = game.Services.GetService(typeof(UIViewController)) as UIViewController;
var view = new YourView();
gameController.View.AddSubview(view);

You'll need to add some logic to pause the game if you take this route. I've used this option before for iAd, etc. You could also do this with other controllers' views, like your tab controller example above.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • Hey, sorry for the delayed response. I'm going to have to try your way as I just can't get it to work using UIKit first and then sprinkling the MonoGame stuff in. I do have a question though, how does doing this effect memory? Is it very bogged down this way? – halterdev May 15 '14 at 20:33
  • It should be fine. Just make sure to dispose and removefromsuperview when no longer using UIKit stuff. – jonathanpeppers May 15 '14 at 22:48
  • Did this answer work out for you? You might take the time to mark as answer, or add a new comment. Thanks. – jonathanpeppers Aug 06 '14 at 19:06