从子类加载纹理

时间:2013-08-31 00:23:11

标签: c# xna content-pipeline

我正在制作Windows Phone游戏,而且我是XNA的初学者。我的问题在于我的主文件中的Content.RootDirectory = "Content";。当我开始调试时,它告诉我在我的子类文件中,当我放入另一个文件时,找不到内容中的徽标

  

Content_logo.RootDirectory =“内容”;

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

如果我尝试创建另一个根目录,则会显示错误,指出在将内容加载到ContentManager后无法更改此属性。我的问题是如何从子类的内容目录中加载我的纹理。

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);
    }
}
}

这是我的子类Logo,这是我在屏幕上显示的第一个枚举器..

 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);
    }
}
}

调试停止,因为它无法找到徽标,我的图像格式为.png,因此不是格式。

3 个答案:

答案 0 :(得分:1)

当XNA编译时,它会将所有资源(纹理,声音,音乐,字体等)导入并处理为.xnb文件。纹理的标准文件类型是.png。

如果找不到纹理,可能意味着它没有被导入。通过右键单击ProjectNameContent(Content)添加内容 - &gt;添加 - &gt;解决方案资源管理器中的现有项目。

完成此操作后,请确保纹理的Content Importer和Content Processor属性都设置为“Texture - XNA Framework”。

在旁注中,您在每个步骤中从MainFile.Update()中调用Logo.Load() - 更改此信息可能是个好主意。

答案 1 :(得分:0)

创建另一个内容管理器不是解决这个问题的方法。

解决此问题的一种方法是将Content的引用作为方法的参数传递。例如:

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

public class Game1 : Game
{
     //...
     protected override void LoadContent()
     {
          Foo.LoadContent(Content);
     }
}
编辑:还有,你是通过VS加载内容吗?右键单击[ProjectName]内容(内容) - &gt;添加 - &gt;现有物品?只是检查......

答案 2 :(得分:0)

如果你需要在子类中使用Content,你可以将Content传递给该类,或者可以将该类设置为从GameComponent继承,这样你就可以简单地访问使用Game.Content的内容管理员 您无需声明其他内容。