Create your first form

Creating your first form is really simple. It only takes three lines of code to get it up and running in your game. Make sure you have followed the guide to preparing your game with X-Forms first.

In order to add a Form to your game, you must first Instance a copy. You do that by adding the following line in your games Initialize() method.

        protected override void Initialize()
        {
            StandardForm form = new StandardForm(this, "New Form");

            base.Initialize();
        }

You can replace "New Form" with what ever you want to name your Form. The name you provide is what is displayed on the Forms title bar. The Forms name and text that is displayed can be different from each other, but we will cover that in a later tutorial.

Now that you have instanced the Form, you need to add it to your games Components collection. XNA Components have their Initialize, Update and Draw methods invoked automatically for you. So once you add your Form to your games Components collection, you won't have to bother with adding it to your update loop or game loop.

        protected override void Initialize()
        {
            StandardForm form = new StandardForm(this, "New Form");
            this.Components.Add(form);

            base.Initialize();
        }

It's important to instance your Form before calling base.initialize().

Lastly, we need to display our Form to the screen. You do that by invoking it's Show() method.

        protected override void Initialize()
        {
            StandardForm form = new StandardForm(this, "New Form");
            this.Components.Add(form);
            
            base.Initialize();

            form.Show();
        }

It's important to invoke the Show() method after you have called base.Initialize().

There you have it! A Form displayed in your game. Granted there's nothing fancy about it, but you can click on it's title bar and drag the Form around.

Over the next few tutorials we will work on creating a customized Form that doesn't look so bland and actually has some functionality to it.

Last edited Jan 19, 2012 at 8:57 PM by Scionwest, version 3

Comments

No comments yet.