Transferring Texture Across Classes

旧巷老猫 提交于 2019-12-11 20:22:31

问题


(Resizing and LoadContent())

I want to separate textures by having my Title Screen be in a class and call on it when needed. This would help with neatness but I have only been working with moving sprites across classes and Texture2D is not the same.

I have an image 1024X768 and am calling on the image in Title Screen.cs and want to be able to use LoadContent() in Title Screen.cs to load anything with out having to go to Game1.cs to Load the image and come back to Title Screen.cs. Using load content in Title Screen.cs will make my code easier to read and understand. It needs to fit the screen as well and is not, I would like to know what function I could use to re size it.

(I have a function now from resizing the image but it is only a manual fix. I must input the correct width of the screen, is there a simple way to check the screen height and width?)


EDIT: I have now fix the code to be updated to the new version.

I have the normal start up for the main class, Game1.cs:

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;

namespace Title_Test
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        Title_Screen.Content = Content;
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

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

        Title_Screen.Load();
    }

    protected override void UnloadContent()
    {
    }

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

        base.Update(gameTime);
    }

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

        Title_Screen.Draw(spriteBatch);

        base.Draw(gameTime);
    }
}
}

Title Screen.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace Title_Test
{
class Title_Screen
{
    public static ContentManager Content;
    public static Texture2D titleScreenPH;//Texture to be transferred.
    Vector2 TitleScreen = new Vector2(0, 0);

    public static int myWidth = 800;
    public static int myHeight = 480;

    public static void Load()
    {
        titleScreenPH = Content.Load<Texture2D>(@"Images\TitleScreenPH");
    }

    static public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(titleScreenPH, new Rectangle(0, 0, myWidth, myHeight), Color.White);
        spriteBatch.End();
    }
}
}

回答1:


If you need to change the drawing size of your sprite just give look to the overloads of the Draw() method of the SpriteBatch class:

spriteBatch.Draw(titleScreenPH, new Rectangle(0, 0, myWidth, myHeight), Color.White);

EDIT:

I have an image 1024X768 and am calling on the image in Title Screen.cs and want to be able to use LoadContent() in Title Screen.cs to load anything with out having to go to Game1.cs to Load the image and come back to Title Screen.cs. Using load content in Title Screen.cs will make my code easier to read and understand.

I wouldn't use static members in this case, althought you could avoid it in this way:

class Title_Screen{
    //...
    public static ContentManager Content;
    Texture2D titleScreenPH;

    //...
    public static void Load(){
       titleScreenPH = Content.Load<Texture2D>(@"Images\TitleScreenPH");
    }
}

Then:

public class Game1 : Microsoft.Xna.Framework.Game{
    //...

    public Game1(){
        //...
        Title_Screen.Content = Content;
    }

    protected override void LoadContent(){
        //...
        Title_Screen.Load();
    }
}

EDIT 2:

I have a function now from resizing the image but it is only a manual fix. I must input the correct width of the screen, is there a simple way to check the screen height and width?

Yes you can get the screen size from the GraphicsDevice.PresentationParameters property:

int screenWidth = graphicsDevice.PresentationParameters.BackBufferWidth;
int screenHeight = graphicsDevice.PresentationParameters.BackBufferHeight;


来源:https://stackoverflow.com/questions/20001894/transferring-texture-across-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!