My Full Example of GeonBit & Classes

From TRCCompSci - AQA Computer Science
Revision as of 10:37, 18 October 2017 by Admin (talk | contribs) (Created page with "=Game1.cs enum= I have created the following enum within the Game1 class: <syntaxhighlight lang=csharp> public enum GameState { Screen1,...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Game1.cs enum

I have created the following enum within the Game1 class:

        public enum GameState
        {
            Screen1,
            Screen2
        }

        private GameState state=GameState.Screen1;

        public GameState State
        {
            get { return state; }
            set
            {
                state = value;
                SwitchScreen();
            }
        }

wheneever the enum is changed it will run my SwitchScreen() method. I will show this method later. Before this we will need to create the classes for our base screen and for the actual screens.

Screen base class

    class UIScreen
    {
        public Panel window;
        public int width, height;
        public Game1 parentGame;
      
        public UIScreen(PanelSkin p, Anchor a, int w, int h)
        {
            width = w;
            height = h;
            window = new Panel(new Vector2(w, h), p,a);
            UserInterface.Active.AddEntity(window);
        }

        public void show(bool b)
        {
            window.Visible = b;
        }

        public void drag(bool b)
        {
            window.Draggable = b;
        }
    }

The variables will allow you to set and remember the height & width, the parentGame will be used in any subclass to allow the screen to access and change the enum we have created. I have added a methods to allow you to set the Visible & Draggable setting of the screen. I have also created a constructor which will create the actual window using the settings passed.

Screen class

class Screen1:UIScreen
    {
        public Screen1(PanelSkin p, Anchor a, int w, int h):base(p,a,w,h)
        {
          
        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("Screen 1");
            window.AddChild(title);

            Paragraph test = new Paragraph("Welcome to screen 1");
            window.AddChild(test);

            Button button = new Button("Click");
            window.AddChild(button);

            button.OnClick = (Entity btn) =>
            {
                test.Text = "Screen 1 - button clicked";
                //base.show(false);
            };

            Button button1 = new Button("Close");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Screen2;
                window.ClearChildren();
            };
        }
    }

the constructor class requires you to specify parameters for each of the items within the base screen constructor. The screen constructor just passes the values into the base constructor.

The Init method requires a parameter for the game. This means the later button1.OnClick can access the enum to switch screens. This code creates a header, paragraph, a button to carry out an action on the paragraph, and a button to access the enum and switch screens.

Note you need to do this for each screen

Creating screens in Game1.cs

You will need to declare variables for your screens

<syntaxhightlight lang=csharp>

       Screen1 test;
       Screen2 test1;

</syntaxhighlight>