Need help with interaction with my classes.

I am very new to MonoGame and C# and I am really struggling to get my main code to interact with the class I’ve created.
The core idea is that I want a bar to slowly decrease as time passes.
I have tried so many things and now it’s all a big mess now.
If anyone has any advice or solutions I would really like to hear them.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace Vpet_001
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        private Texture2D _texture;
        private Vector2 _position;
        private Rectangle _barState;
        private int _spriteX;
        private int _spriteY;
        private int _spriteWidth;
        private int _spriteHeight;
        public BarClock hunger;
        

        public SpriteFont gameFont;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;            
            
    }

        protected override void Initialize()
        {
            

            base.Initialize();
        }

        protected override void LoadContent()
        {
            gameFont = Content.Load<SpriteFont>("galleryFont");
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _texture = Content.Load<Texture2D>("Bar-Sheet");
            _position = new Vector2(10, 10);
            _barState = new Rectangle(_spriteX, _spriteY, _spriteWidth, _spriteHeight);

            hunger = new BarClock();
            hunger.aBarMax = 6;
            hunger.aBarCurrent = 6;


        }

        protected override void Update(GameTime gameTime)
        {
            hunger.BarState();
            if (hunger.barState == 6)
            {                          
                _spriteX = 0;
                _spriteY = 0;
                _spriteWidth = 100;
                _spriteHeight = 22;
                _barState.X = _spriteX; 
                _barState.Y = _spriteY;
                _barState.Width = _spriteWidth;
                _barState.Height = _spriteHeight;                 
            }
            
            


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            _spriteBatch.Draw(_texture, _position, _barState, Color.White);

            //_spriteBatch.DrawString(gameFont, hunger.barState, new Vector2(500, 10), Color.White);
            //_spriteBatch.DrawString(gameFont, gameDay.ToString(), new Vector2(500, 40), Color.White);


            _spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}

This is the class im trying to use


`using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Vpet_001
{
    public class BarClock
    {
        private int gameTime = 0;                    //counting up every frame of the program 
        public static int gameDayLength = 30;       //How many frames it takes to equal 1 Day
        private int gameDay = 0;                         //represents the current day number 
        private int barDurration;                    //how many days it takes for I segment of the bar to be depleted
        public int barMax;                          //the maximum number of segments in the bar
        private static int barMin = 0;               //the minimum number of segments in a bar
        public  int barCurrent;                      //the current amount of segments in the bar
        public int barState;

        public int aBarCurrent;
        public int aBarMax;
              
        public void BarState()

        {            
            barCurrent = aBarCurrent;
            barMax = aBarMax;
            while (barCurrent > barMin && barCurrent <= barMax)
            {
                gameTime++;

                if (gameTime == gameDayLength)
                {
                    gameDay++;
                    barCurrent--;
                    gameTime = 0;
                    
                }                         
            }

           
            barState = barCurrent;
            
        }     

    }  
}````

Hey @Atomic0094

I rewrote a bit of the code and provided comments as to why I changed certain things to the way I did.

If you have any questions, please let me know rather than just accepting this code blindly.

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Vpet_001
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        private Texture2D _texture;
        private Vector2 _position;
        private Rectangle _barState;
        public BarClock hunger;


        public SpriteFont gameFont;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;

        }

        protected override void Initialize()
        {
            base.Initialize();

            //  Moved the following stuff to Initialize instead of LoadContent.
            //  Since these are initializations and not content loading.

            _position = new Vector2(10, 10);

            //  Originally use created this rectangle using the _spriteX, _spriteY, _spriteWidth, and
            //  _spriteHeight, but you never gave them value before this point, so they would
            //  have just been zero anyway, or Rectangle.Empty
            _barState = Rectangle.Empty;

            hunger = new BarClock();
            hunger.aBarMax = 6;
            hunger.aBarCurrent = 6;
            hunger.barCurrent = 6;

            //  You only ever set these values once originally, so I moved them here
            //  to only be set once
            _barState.X = 0;
            _barState.Y = 0;
            _barState.Width = 100;
            _barState.Height = 22;
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            gameFont = Content.Load<SpriteFont>("galleryFont");

            // _texture = Content.Load<Texture2D>("Bar-Sheet");

            //  I didn't have your "bar-sheet" texture so I just created a 1x1 white pixel texture 
            //  to test with
            _texture = new Texture2D(GraphicsDevice, 1, 1);
            _texture.SetData<Color>(new Color[] { Color.White });
        }

        protected override void Update(GameTime gameTime)
        {
            hunger.Update(gameTime);

            //  Calculate the width of the bar
            _barState.Width = (int)(100.0f * (hunger.barState / (float)hunger.barMax));

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();

            //  Use the destination rectangle parameter here since the _barState is a rectangle
            //  that defines the size of the bar to draw
            _spriteBatch.Draw(_texture, _barState, null, Color.White);

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

BarClock.cs

using Microsoft.Xna.Framework;
using System;

namespace Vpet_001
{
    public class BarClock
    {
        //  The minimum number of segments in a bar.
        //  Made this a const since this isn't something you're ever changing and seemed
        //  to only be used as a const check anyway.
        private const int BAR_MIN = 0;  


        //  Changed the game day length and the game time to be TimeSpans
        //  so we can track time using the GameTime provided through the
        //  update methods 


        //  How much time equals 1 day
        //  Original was 30, meaning 30 frames
        //  if we assume 60fps,then every 30 frames would every 1/2 second or 500 ms
        private static TimeSpan s_gameDayLength = TimeSpan.FromMilliseconds(500);

        //  Counting up every frame of the program
        private TimeSpan _gameTime;

        //  Represents the current day number
        private int _gameDay = 0;

        //  How many days it takes for 1 segment of the bar to be depleted
        //  This was never used or set in your original program so I'm just going to put
        //  it at 2 days? I dunno
        private int _barDuration = 2;

        //  The maximum number of segments in the bar
        public int barMax;

        //  The current amount of segments
        public int barCurrent;

        public int barState;

        public int aBarCurrent;
        public int aBarMax;


        //  Changed the name of this method to Update since that's what this method is
        //  is doing...it's updating the BarClock
        public void Update(GameTime gameTime)
        {
            barMax = aBarMax;

            //  Increment by the total amount of time that has passed since the last frame
            _gameTime += gameTime.ElapsedGameTime;

            //  Check if we should deplete the bar
            if (barCurrent > BAR_MIN && barCurrent <= barMax && _gameTime >= s_gameDayLength)
            {
                _gameDay++;

                //  Only deplete the bar if the correct number of days has passed
                if (_gameDay % _barDuration == 0)
                {
                    barCurrent--;
                }

                _gameTime = TimeSpan.Zero;
            }

            barState = barCurrent;
        }
    }
}
2 Likes

Thank you!
I’ve gone over everything you did and understand what you have done.
You’re a legend

1 Like