10 Replies - 11103 Views - Last Post: 30 July 2011 - 04:41 PM

#1 Ukurac   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 11-July 11

Problem with components

Posted 30 July 2011 - 07:45 AM

Hello again :)

Im trying to make a start screen and a menu for my game. But i got this problem which i dont know how to solve..

Every time i start my project it throws an exception. Here's the whole code

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace SpaceCommander.Components
{
    
    public class GameScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        List<GameComponent> childComponents;
        
        public GameScreen(Game game)
            : base(game)
        {
            childComponents = new List<GameComponent>();
            Visible = true;
            Enabled = true;

        }

        public List<GameComponent> Components
        {
            get {return childComponents;}
            
        }
     
     
        
      
        public override void Initialize()
        {
         
            base.Initialize();
        }

      
      
        
        
        public override void Update(GameTime gameTime)
        {
          
            foreach ( GameComponent child in childComponents)
            {
                if (child.Enabled)
                {
                    child.Update(gameTime);
                }
            }
      




            base.Update(gameTime);
        }

        public  virtual void Show()
        {
            Visible = true;
            Enabled = true;
        }
        public virtual void Hide()
        {
            Visible = false;
            Enabled = false;
        }





        public override void Draw(GameTime gameTime)
        {

            foreach (DrawableGameComponent child in childComponents)
            {
                child.Draw(gameTime);
            }
                
                    
            
            
            
            
            
            base.Draw(gameTime);
        }

    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace SpaceCommander.Components
{
   public class StartScreen: GameScreen
    {

        Menu menu;

        public StartScreen(Game game, Texture2D texture, SpriteFont spriteFont)
            : base(game)
        {

           
            string[] items = { "Single Player","Options", "Quit" };
            menu = new Menu(game, spriteFont);
            menu.SetMenuItems(items);
            Components.Add(menu);




        }

        public int SelectedIndex
        {
            get { return menu.SelectedIndex; }
        }
        public override void Show()
        {
            menu.position = new Vector2(
            (800 - menu.width) / 2, 330);
            base.Show();
        }
        public override void Hide()
        {
            base.Hide();
        }
    }
}





using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections.Specialized;

namespace SpaceCommander.Components
{
   

    public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch = null;
        SpriteFont spriteFont;
        Color normal = Color.Yellow;
        Color helistate = Color.Red;
        public Vector2 position = new Vector2();
        int selectedIndex = 0;

        public StringCollection menuItems = new StringCollection();
        public int width, heigh;
        KeyboardState oldState;



        public Menu(Game game, SpriteFont spriteFont)
            : base(game)
        {
            this.spriteFont = spriteFont;
            spriteBatch = 
(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
        }



        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = (int)MathHelper.Clamp(
                value,
                0,
                menuItems.Count - 1);
            }
        }


        public void SetMenuItems(string[] items)
        {
            menuItems.Clear();
            menuItems.AddRange(items);
            CalculateBounds();
        }



        private void CalculateBounds()
        {
            width = 0;
            heigh = 0;
            foreach (string item in menuItems)
            {
                Vector2 size = spriteFont.MeasureString(item);
                if (size.X > width)
                    width = (int)size.X;
                heigh += spriteFont.LineSpacing;
            }
        }


        public bool CheckKey(Keys theKey)
        {
            KeyboardState newState = Keyboard.GetState();
            return oldState.IsKeyDown(theKey) && newState.IsKeyUp(theKey);
        }



        public override void Initialize()
        {
           

            base.Initialize();
        }

       
        public override void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();
            if (CheckKey(Keys.Down))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Count)
                    selectedIndex = 0;
            }
            if (CheckKey(Keys.Up))
            {
                selectedIndex--;
                if (selectedIndex == -1)
                {
                    selectedIndex = menuItems.Count - 1;
                }
            }
            oldState = newState;
           
            
            
            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            
            Vector2 menuPosition = position;
Color myColor;
for (int i = 0; i < menuItems.Count; i++)
{
    if (i == SelectedIndex)
        myColor = helistate;
    else
        myColor = normal;
   
    spriteBatch.DrawString(
    spriteFont,
    menuItems[i],
    menuPosition + Vector2.One,
    Color.Black);
    spriteBatch.DrawString(spriteFont,
    menuItems[i],
    menuPosition,
    myColor);
    
    menuPosition.Y += spriteFont.LineSpacing;
}
            
            
            base.Draw(gameTime);
        }


    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections.Specialized;

namespace SpaceCommander.Components
{
   

    public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch = null;
        SpriteFont spriteFont;
        Color normal = Color.Yellow;
        Color helistate = Color.Red;
        public Vector2 position = new Vector2();
        int selectedIndex = 0;

        public StringCollection menuItems = new StringCollection();
        public int width, heigh;
        KeyboardState oldState;



        public Menu(Game game, SpriteFont spriteFont)
            : base(game)
        {
            this.spriteFont = spriteFont;
            spriteBatch = 
(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
        }



        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = (int)MathHelper.Clamp(
                value,
                0,
                menuItems.Count - 1);
            }
        }


        public void SetMenuItems(string[] items)
        {
            menuItems.Clear();
            menuItems.AddRange(items);
            CalculateBounds();
        }



        private void CalculateBounds()
        {
            width = 0;
            heigh = 0;
            foreach (string item in menuItems)
            {
                Vector2 size = spriteFont.MeasureString(item);
                if (size.X > width)
                    width = (int)size.X;
                heigh += spriteFont.LineSpacing;
            }
        }


        public bool CheckKey(Keys theKey)
        {
            KeyboardState newState = Keyboard.GetState();
            return oldState.IsKeyDown(theKey) && newState.IsKeyUp(theKey);
        }



        public override void Initialize()
        {
           

            base.Initialize();
        }

       
        public override void Update(GameTime gameTime)
        {
            KeyboardState newState = Keyboard.GetState();
            if (CheckKey(Keys.Down))
            {
                selectedIndex++;
                if (selectedIndex == menuItems.Count)
                    selectedIndex = 0;
            }
            if (CheckKey(Keys.Up))
            {
                selectedIndex--;
                if (selectedIndex == -1)
                {
                    selectedIndex = menuItems.Count - 1;
                }
            }
            oldState = newState;
           
            
            
            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            
            Vector2 menuPosition = position;
Color myColor;
for (int i = 0; i < menuItems.Count; i++)
{
    if (i == SelectedIndex)
        myColor = helistate;
    else
        myColor = normal;
   
    spriteBatch.DrawString(
    spriteFont,
    menuItems[i],
    menuPosition + Vector2.One,
    Color.Black);
    spriteBatch.DrawString(spriteFont,
    menuItems[i],
    menuPosition,
    myColor);
    
    menuPosition.Y += spriteFont.LineSpacing;
}
            
            
            base.Draw(gameTime);
        }


    }
}



If any1 could take a look at it, i tried everything. Also i think the problem is in draw method somewhere, because when i put Enabled to true and Visible to false, it doesnt throw an exception, only when i put visible to true.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with components

#2 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with components

Posted 30 July 2011 - 08:17 AM

What is the exception?

Also you've posted the menu class twice, is there meant to be another class?

Edit: I can tell that you will never be able to draw any text as your LoadContent() is missing and you haven't loaded in a spriteFont.

This post has been edited by ShadowsEdge19: 30 July 2011 - 08:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ukurac   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 11-July 11

Re: Problem with components

Posted 30 July 2011 - 08:59 AM

Yea i forgat to add Game class sorry

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpaceCommander.Components;
namespace SpaceCommander
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Player player;
        RandomGenerator random= new RandomGenerator();
        Texture2D ship;
        TileEngine tile;
        Texture2D background;
        Vector2 pozadina;
        StartScreen startScreen;
        SpriteFont spriteFont;
        int sirina, visina;
       
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        

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




        protected override void LoadContent()
        {
           
             visina = graphics.PreferredBackBufferHeight;
             sirina = graphics.PreferredBackBufferWidth;
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            spriteFont = Content.Load<SpriteFont>(@"Fonts/MenuFont");
            background = Content.Load<Texture2D>(@"Sprites/bsg-stars");
            startScreen = new StartScreen(this, background, spriteFont);
            Components.Add(startScreen);
            
            



            ship= Content.Load<Texture2D>(@"Sprites/spaceship");
            tile = new TileEngine(background, 1920, 1200);
            player = new Player(random.randomVector2(300, 200), ship, 2);

            


        }

      


        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

      



        protected override void Update(GameTime gameTime)
        {
           
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();


            Camera();

           
         
            //player.update(gameTime);
            base.Update(gameTime);
        }

        public void Camera()
        {
            pozadina.X++;
            
            if (player.Position.X > sirina - player.Texture.Width)

                player.Position.X = sirina - player.Texture.Width;
            if (player.Position.X < 0)
                player.Position.X = 0;
            if (player.Position.Y < 0)
                player.Position.Y = 0;

        }

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

            tile.draw(spriteBatch, (int)pozadina.X, (int)pozadina.Y);
          // player.draw(spriteBatch, gameTime);
            base.Draw(gameTime);
            spriteBatch.End();

        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with components

Posted 30 July 2011 - 09:35 AM

Have you changed your Player, RandomGenerator or Camera class since you last uploaded your project? If not then I can update your original version with these newer classes, though I would also need the TileEngine class as well.

This post has been edited by ShadowsEdge19: 30 July 2011 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Problem with components

Posted 30 July 2011 - 12:00 PM

What exception is being thrown? Is it a null reference exception or something else? Narrowing down what the exception is will help us figure out what the exact problem is. I saw something that may be causing the problem but with out seeing what exception is being thrown it is hard to know for sure.
Was This Post Helpful? 0
  • +
  • -

#6 Ukurac   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 11-July 11

Re: Problem with components

Posted 30 July 2011 - 01:42 PM

An unhandled exception of type 'System.NullReferenceException' occurred in SpaceCommander.exe

Additional information: Object reference not set to an instance of an object.

And this part is marked yellow.

 spriteBatch.DrawString(
    spriteFont,
    menuItems[i],
    menuPosition + Vector2.One,
    Color.Black);

Was This Post Helpful? 0
  • +
  • -

#7 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with components

Posted 30 July 2011 - 03:18 PM

That would mean that either menuItems or one of the individual menu items was not initialized correctly, and as far as I can tell menuItems was but I'm not sure if the item itself was:

string[] items = { "Single Player","Options", "Quit" };


Shouldn't that be:

string[] items = new string[] { "Single Player","Options", "Quit" };

Was This Post Helpful? 0
  • +
  • -

#8 Ukurac   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 11-July 11

Re: Problem with components

Posted 30 July 2011 - 03:45 PM

Nah its still the same problem. However i think that maybe spriteBatch was not properly initialized.
What do you think?
Was This Post Helpful? 0
  • +
  • -

#9 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with components

Posted 30 July 2011 - 03:58 PM

Try replacing what you have for spriteBatch in game1 LoadContent() with:

spriteBatch = new SpriteBatch(GraphicsDevice);
Services.AddService(typeof(SpriteBatch), spriteBatch);


Was This Post Helpful? 2
  • +
  • -

#10 Ukurac   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 11-July 11

Re: Problem with components

Posted 30 July 2011 - 04:06 PM

You sir are an genius!
Its working now! Thank you so much. I thought it has something to do with spritebatch but didnt knew how to fix it :)
Was This Post Helpful? 0
  • +
  • -

#11 ShadowsEdge19   User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: Problem with components

Posted 30 July 2011 - 04:41 PM

I've an eye for detail and have experienced the very same errors you have.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1