2010-12-03 51 views
6

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

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

但我也读到纹理加载以这种方式忽略了ContentManager,而我正在做垃圾回收器的工作更加困难。

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

上下文: 我正在创建一个n×n和平板。当n太大时,我需要自动变为更小。

+0

乔的回答是正确的。另外:无论您是否使用ContentManager,垃圾收集器都没有区别。它只影响你如何卸载纹理,如果这是你的游戏需要的东西(例如:当在不同层次间变化时)。看看我的答案在这里:http://stackoverflow.com/questions/4264995/how-do-i-unload-content-from-the-content-manager/4265786#4265786。 – 2010-12-04 01:24:07

回答

10

要载入纹理:

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的一个,我建议你阅读this short tutorial,还可以在create.msdn.com查看Education Catalog

+0

谢谢你,它的工作原理就像我想要的那样:)...现在关于如果使用或不使用我提到的方法,你认为是安全的,还是他的使用是其他的东西? – mjsr 2010-12-03 20:55:22

0
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); 
     } 
+1

欢迎来到Stack Overflow!仅有代码的答案不是很有帮助。请编辑您的答案,以解释为什么您的代码可以解决原始问题。 – 2017-01-18 22:13:04

相关问题