Loading Texture from subclass

303 views Asked by At

I'm making a Windows Phone game and I'm a beginner in XNA. My Problem is with the Content.RootDirectory = "Content"; that is in my main file. When I start the debugging, it tell me that in my subclass files can't find the logo that is in the Content when I put another

Content_logo.RootDirectory= "Content";

Logo_Texture2d = Content_logo.Load<Texture2D>("Logo");

If I try making another root Directory an error shows up saying that This property cannot be changed after content has been loaded into the ContentManager. My question is how can I load up my texture from the content directory from a subclass.

public class MainFile : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    ScreenStates screentState;

    Rectangle TouchS_Y_X;

    Logo logo;
    Menu0 menu;
    Choose_Pets choose_pets;
    ScreenStates.CurrentGameState GameState;

    public MainFile()
    {
        graphics = new GraphicsDeviceManager(this);

        Content.RootDirectory = "Content";

        // Frame rate is 30 fps by default for Windows Phone.
        TargetElapsedTime = TimeSpan.FromTicks(333333);

        // Extend battery life under lock.
        InactiveSleepTime = TimeSpan.FromSeconds(1);

        this.screentState = new ScreenStates();
        ///choose_pets = new Choose_Pets();
        choose_pets = new Choose_Pets();

        logo = new Logo();

        menu = new Menu0();
    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        GameState = ScreenStates.CurrentGameState.Logo;
        base.Initialize();

    }


    protected override void LoadContent()
    {
        TouchS_Y_X = new Rectangle(0, 0, 1, 1);
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        this.menu.Load_Menu(GraphicsDevice, Content);
        choose_pets.Load_ChoosePet(Content, GraphicsDevice);

        // TODO: use this.Content to load your game content here
        base.LoadContent();
    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
        //this.logo.Unload_logo(Content);
    }


    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        #region Games States

        switch (GameState)
        {
            case ScreenStates.CurrentGameState.Logo:

                    logo.Update_logo(gameTime);

                    this.logo.Load(this.Content, this.GraphicsDevice);

                    if (logo.FadeOut_logo == true)
                    GameState = ScreenStates.CurrentGameState.Menu;

                    break;

            case ScreenStates.CurrentGameState.Menu:
                    menu.Update_Menu(gameTime);

                break;

            case ScreenStates.CurrentGameState.CharactersChooser:
                    //choose_pets.Update_petchoose(gameTime);
                break;
        }
        #endregion

        // TODO: Add your update logic here
        base.Update(gameTime);
    }


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

        #region GameStateDraw
        spriteBatch.Begin();

        switch(GameState)
        {
            case ScreenStates.CurrentGameState.Logo:           
                logo.Draw(spriteBatch);
            break;

                //Menu Draw State
            case ScreenStates.CurrentGameState.Menu:
             menu.Draw_Menu(spriteBatch);
            break;

            case ScreenStates.CurrentGameState.CharactersChooser:
               // choose_pets.Draw_petChoose(spriteBatch);
            break;
        }
        spriteBatch.End();
 #endregion

        base.Draw(gameTime);
    }
}
}

And this is my subclass Logo that is my first enumerator to be show in the screen..

 class Logo
{
    Texture2D Logo_Texture2d;
    Rectangle Logo_Rec;
    Color Logo_color;

    public bool FadeOut_logo = false;

    public double _Timer_logo;

    SpriteFont Norm_fonts;

    public void Load(ContentManager Content_logo, GraphicsDevice graphics_logo)
    {
        Logo_Texture2d = Content_logo.Load<Texture2D>("Logo");
        Logo_Rec = new Rectangle(0, 0, graphics_logo.Viewport.Width, graphics_logo.Viewport.Height);

        Norm_fonts = Content_logo.Load<SpriteFont>("Fonts\\Normal_Font"); 
    }

    //public void Unload_logo(ContentManager Content_logo)
   // {
      //  if (FadeOut_logo == true)
      //      Content_logo.Unload();
    //}

    public void Update_logo(GameTime gametime_logo)
    {

        _Timer_logo += gametime_logo.ElapsedGameTime.TotalSeconds;

        if (gametime_logo.ElapsedGameTime.TotalSeconds >= 10)
            Logo_color.A--; Logo_color.B--; Logo_color.G--; Logo_color.R--;

        if (Logo_color.B == 0)
            FadeOut_logo = true;

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Logo_Texture2d, Logo_Rec, Logo_color);
        spriteBatch.DrawString(Norm_fonts, "CurrentState: Logo" + "    Timer: " + ((int)_Timer_logo).ToString(), new Vector2(0, 0), Color.White);
    }
}
}

the debugging stop because it cant find the logo and my image format is .png so is not the format.

3

There are 3 answers

2
Sean Latham On BEST ANSWER

When XNA compiles, it imports and process all assets - textures, sound, music, fonts, etc. - into .xnb files. The standard filetype for texture is .png.

If the texture cannot be found, it probably means that it is not being imported. Add content by right-clicking ProjectNameContent (Content) -> Add -> Existing Item in the Solution Explorer.

When you've done this, make sure that the Content Importer and Content Processor properties of the texture are both set to "Texture - XNA Framework".

On a side note, you are calling Logo.Load() from within MainFile.Update() upon each step - it might be a good idea to change this.

2
Sean Latham On

Creating another content manager is not the way to get around this.

One way to solve this would to be to pass a reference to Content as a parameter of a method. For example:

class Foo
{
     public void LoadContent(ContentManager content)
     {
          Logo_Texture2d = content.Load<Texture2D>("Logo");
     }
}

public class Game1 : Game
{
     //...
     protected override void LoadContent()
     {
          Foo.LoadContent(Content);
     }
}

EDIT: also, are you loading in content through VS? Right-click [ProjectName]Content (Content) -> Add -> Existing Item? Just checking...

1
pinckerman On

If you need to use Content in a subclass, you sholud pass the Content to that class, or maybe set that class to inherit from GameComponent, so you can simply access the content manager using Game.Content.
You don't need to declare another content.