7

我是 XNA 的新手,以防万一。我尝试做的是加载与他的原始大小不同的纹理,或者至少有可能在之后改变他的大小。我在一些可以使用的地方看到:

Texture2D.FromStream(GraphicsDevice graphicsDevice, Stream stream, 
                 int width, int height, bool zoom)

但我也读到以这种方式加载纹理忽略了 ContentManager,并且我让垃圾收集器的工作变得更加困难。

使用 ContentManager 加载任何大小的图像的正确方法是什么?如果那不可能,我可以按比例改变他的大小,比如使用缩放吗?

背景:我正在创建一个 nxn 和平委员会。当 n 太大时,我需要自动和平变得更小。

4

2 回答 2

12

加载纹理:

Texture2D tex = Content.Load<Texture2D>("somefile");

要调整它的大小,请使用采用“缩放”的 SpriteBatch 重载之一 http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

float scale = .5f; //50% smaller
SpriteBatch.Draw(tex, position, source, Color.White, rotation, scale, SpriteEffects.None, 0f);

如果您是 XNA 的新手,我建议您阅读这个简短的教程,并查看create.msdn.com 上的教育目录

于 2010-12-03T20:38:20.403 回答
1
Texture2D texture;
protected override void LoadContent()
        {
...
         texture = Content.Load<Texture2D>("Tank");
...
        }
protected override void Draw(GameTime gameTime)
        {
...
         Rectangle destinationRectangle = new Rectangle(100, 100, 30, 10);
         spriteBatch.Draw(texture, destinationRectangle, Color.White);
...
         spriteBatch.End();
         base.Draw(gameTime);
        }
于 2017-01-18T22:10:22.580 回答