跨类传输纹理 [英] Transferring Texture Across Classes

查看:33
本文介绍了跨类传输纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(调整大小和LoadContent())

我想通过让我的标题屏幕在一个类中并在需要时调用它来分离纹理.这将有助于整洁,但我一直在处理跨类移动精灵,而 Texture2D 不一样.

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.

我有一张 1024X768 的图像,我正在 Title Screen.cs 中调用该图像,并希望能够在 Title Screen 中使用 LoadContent().cs 加载任何内容,而不必转到 Game1.cs 加载图像并返回 Title Screen.cs.在 Title Screen.cs 中使用加载内容将使我的代码更易于阅读和理解.它也需要适合屏幕,但不是,我想知道我可以使用什么功能来调整它的大小.

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?)

我现在已经修复了要更新到新版本的代码.

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

我可以正常启动主类 Game1.cs:

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

标题 Screen.cs:

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

推荐答案

如果您需要更改精灵的绘图大小,只需查看 Draw() SpriteBatch 方法代码>类:

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

<小时>

我有一张 1024X768 的图像,我正在调用标题中的图像Screen.cs 并希望能够在 Title Screen.cs 中使用 LoadContent()加载任何东西而不必去 Game1.cs 加载图像然后回到 Title Screen.cs.在标题中使用加载内容Screen.cs 将使我的代码更易于阅读和理解.

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

那么:

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

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

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

<小时>

编辑 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?

是的,您可以从 GraphicsDevice.PresentationParameters 属性:

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

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

这篇关于跨类传输纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆