-4

I'm making a game and I've run into a bit of a snag. From prior research I found a way of altering a variable from multiple classes, but it isn't working. I have set all my screens into an array list and i'm trying to use this variable to change between the screens, but the screen is not changing. there is no error showing up in visual studio, so i'm clueless as to what went wrong. sorry for not being able to point out a certain line. any help is very much appreciated.

this is the class that uses the Int32 GlobalVar.activescreen

public class ManageTheseScreens
{
    ArrayList Screens;
    Screen CurrentScreen;

    public ManageTheseScreens()
    {
        Screens = new ArrayList();
        Screens.Add(new TitleScreen());
        Screens.Add(new OptionScreen());
        CurrentScreen = (Screen)Screens[GlobalVar.activescreen];
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        CurrentScreen.Draw(spriteBatch);
    }
}

this is the class that holds the Int32

    public static class GlobalVar
    {
        public static Int32 activescreen = 0;
    }

this is the class that is trying to change it

class OptionScreen : Screen
{
    Rectangle titleButton1Rectangle = new Rectangle(450, 100, 222, 44);

    MouseState mouseState;

    public OptionScreen()
        : base()
    {

    }

public void Update()
{
    mouseState = Mouse.GetState();
    if (titleButton1Rectangle.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
    {
        if (Mouse.GetState().RightButton == ButtonState.Pressed)
        {
             ///this should switch screens
             GlobalVar.activescreen = 1;
        }
     }
 }
MlkShakes
  • 1
  • 3
  • 3
    whats not working? I am guesing the screen doesnt change? maybe due to the fact you change the variable but do not call anything that checks it after you change it....guessing here as you have not provided enough information – Sorceri Feb 10 '15 at 19:32
  • correct, the screen doesn't change. I don't think i need to call anything that updates it because it's in a Draw function which is called multiple times a second. please do correct me if i'm wrong, but i believe that is how it works. – MlkShakes Feb 10 '15 at 19:37
  • please learn to use a debugger, you should be able to tell us if the value of the variable is in fact changing simply by running the program and looking at it – eddie_cat Feb 10 '15 at 19:53
  • 1
    you draw to the screen but you do not change the screen. CurrentScreen = (Screen)Screens[GlobalVar.activescreen]; needs to be called again once you change the variable. – Sorceri Feb 10 '15 at 20:08
  • @Sorceri Ok, so I put _CurrentScreen = (Screen)Screens[GlobalVar.activescreen];_ into the draw method which will be called many times a second, but the value for _GlobalVar.activescreen_ still isn't changing – MlkShakes Feb 10 '15 at 21:01

1 Answers1

0

you dont need to call CurrentScreen = (Screen)Screens[GlobalVar.activescreen]; in your draw method. In ManageScreen class you should have an update method

Public void UpdateCurrentScreen()
{
    CurrentScreen = (Screen)Screens[GlobalVar.activescreen];
}

in your update method you call

GlobalVar.activescreen = 1;

after you set the var you would call the update method in ManageScreens

public void Update()
{
    mouseState = Mouse.GetState();
    if (titleButton1Rectangle.Contains(new Point(Mouse.GetState().X, Mouse.GetState().Y)))
    {
        if (Mouse.GetState().RightButton == ButtonState.Pressed)
        {
             ///this should switch screens
             GlobalVar.activescreen = 1;
             //variable changes so you now have to update the member CurrentScreen to reflect the change hence the example of below
             manageScreensInstance.UpdateCurrentScreen();
        }
     }
 }

Good Links for static variables and their lifetime C# Static variables - scope and persistence

So is GlobalVar.activescreen always 0? have you put a breakpoint in to ensure it is actually hitting the line of code to change the variable?

Community
  • 1
  • 1
Sorceri
  • 7,870
  • 1
  • 29
  • 38
  • if I add _GlobalVar.activescreen = 1;_ to the update class then it will always be set to 1. the point is to have it change so where i use _GlobalVar.acticescreen_ will equal the array element for the screen i want drawn. I have put a breakpoint in and it is now updating the variable, but the screens still do not switch. – MlkShakes Feb 10 '15 at 21:58
  • @MlkShakes you completely missed what I said. I do not see anywhere in your code other than the constructor where you update the member Currentscreen to reflect the change in the variable. YOU have to set it to the element in the arraylist. It just doesnt do it by itself. – Sorceri Feb 10 '15 at 22:09