简体   繁体   中英

Run windows form while running an XNA game

I am currently designing a RPG game which requires me to utilize an inventory system. Ideally, the user would press a key and make the the character's inventory window open.

example:

protected override void Update(GameTime gameTime)
{
    if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    || (Keyboard.GetState().IsKeyDown(Keys.Escape))) // to exit game, press Esc button
    { this.Exit(); }

    if (Keyboard.GetState().IsKeyDown(Keys.E))
    {
      //insert window from lunch code here...
    }

    // TODO: Add your update logic here
    base.Update(gameTime);
}

I am designing the inventory screen using a window form. However, i have no idea whatsoever on how to load the window form into the Game1 file. Any advise would be appreciated.

您需要在XNA项目中设计库存屏幕,就像创建精灵并在Update方法中捕获用户输入一样。

There are two relatively straightforward routes you can go with this that I could recommend:

  1. Do everything in XNA. This means all your UI (such as inventory window) would have to be done using XNA techniques. This option is going to give you the best design because, let's be honest, WinForms can be a bit ugly especially when they are forced into something that doesn't look anything like a WinForm

  2. Do everything in WinForms. Basically your game would have to be in a WinForm so that all your WinForms can easily communicate through their normal means. The good news is that you can embed an XNA screen inside a WinForm. Basically any XNA level editor tutorial will show you how to do this but here is one example on codeplex and some sample code from Microsoft's collection of examples . From this you should be able to get everything talking. This approach is less than ideal due to performance reasons and aesthetics, but it should be functional if your game is simple enough.

I would highly recommend option one above but if all else fails option two does work.

It would be better if you save yourself the trouble and just draw everything out in XNA. Basically have two classes, one that has all the items, and the other for your inventory. You should also have a List to manage things easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM