2013-02-08 51 views
0

比方說,你這樣做:spriteBatch.Draw(myTexture, myRectangle, Color.White);在XNA中,矩形寬度和精靈紋理的寬度有什麼區別?

而且你有這樣的:

myTexture = Content.Load<Texture2D>("myCharacterTransparent"); 
myRectangle = new Rectangle(10, 100, 30, 50); 

好了,現在我們有30的矩形寬度比方說,myTexture的寬度爲100

所以第一行,它是否使精靈的寬度爲30,因爲這是您設置爲矩形的寬度,而myTexture寬度保持爲100?或者精靈的寬度是否爲100,因爲那是紋理的寬度?

+0

試試吧! ^^如果您對任何3D框架有疑問,只需嘗試所有可能性(例如:紋理>矩形,紋理<矩形和紋理=矩形)。 – Mualig 2013-02-08 13:55:44

+0

我嘗試過,但我很困惑。我想我真正需要知道的是矩形實際上做了什麼以及紋理對精靈有什麼影響。 – CsharpFrustration 2013-02-08 14:06:46

+1

你的問題已經在這裏回答:http://stackoverflow.com/questions/5189833/what-is-source-rectangle-in-spritebatch-draw-in-xna – Mualig 2013-02-08 14:17:08

回答

0

Draw-method使用的矩形定義了什麼部分的Texture2D應該繪製在rendertarget(通常是屏幕)的哪個部分。

這就是我們如何使用tilesets的例子;

class Tile 
{ 
    int Index; 
    Vector2 Position; 
} 

Texture2D tileset = Content.Load<Texture2D>("sometiles"); //128x128 of 32x32-sized tiles 
Rectangle source = new Rectangle(0,0,32,32); //We set the dimensions here. 
Rectangle destination = new Rectangle(0,0,32,32); //We set the dimensions here. 
List<Tile> myLevel = LoadLevel("level1"); 

//the tileset is 4x4 tiles 

in Draw: 
spriteBatch.Begin(); 
foreach (var tile in myLevel) 
{ 
    source.Y = (int)((tile.Index/4) * 32); 
    source.X = (tile.Index - source.Y) * 32; 

    destination.X = (int)tile.Position.X; 
    destination.Y = (int)tile.Position.Y; 

    spriteBatch.Draw(tileset, source, destination, Color.White); 
} 
spriteBatch.End(); 

我可能混淆了其中的矩形在拉伸方法使用的順序,因爲我這樣做了我的頭頂部,而在工作中。

編輯;僅使用源矩形可以讓您在屏幕的某個位置上只繪製一塊紋理,而僅使用目標可以讓紋理縮放以適合所需的任何位置。

相關問題