我知道你可以得到類Texture2d
的寬度和高度,但爲什麼你不能得到x和y座標?我必須爲它們創建單獨的變量嗎?看起來像很多工作。Texture2d的座標?
0
A
回答
1
您必須使用Vector2
對象與Texture2D
對象關聯。 A Texture2D
-object本身沒有任何座標。
當您想繪製紋理時,需要使用SpriteBatch
來繪製紋理,而這需要Vector2D
來確定座標。
public void Draw (
Texture2D texture,
Vector2 position,
Color color
)
這是從MSDN拍攝。
所以,可以創建一個struct
struct VecTex{
Vector2 Vec;
Texture2D Tex;
}
,或者當您需要進一步處理的類。
1
單獨的Texture2D對象沒有任何屏幕x和y座標。
爲了在屏幕上繪製紋理,您必須使用Vector2或矩形設置其位置。
下面是一個使用Vector2一個例子:
private SpriteBatch spriteBatch;
private Texture2D myTexture;
private Vector2 position;
// (...)
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the Texture2D object from the asset named "myTexture"
myTexture = Content.Load<Texture2D>(@"myTexture");
// Set the position to coordinates x: 100, y: 100
position = new Vector2(100, 100);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(myTexture, position, Color.White);
spriteBatch.End();
}
下面是使用矩形的例子:
private SpriteBatch spriteBatch;
private Texture2D myTexture;
private Rectangle destinationRectangle;
// (...)
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the Texture2D object from the asset named "myTexture"
myTexture = Content.Load<Texture2D>(@"myTexture");
// Set the destination Rectangle to coordinates x: 100, y: 100 and having
// exactly the same width and height of the texture
destinationRectangle = new Rectangle(100, 100,
myTexture.Width, myTexture.Height);
}
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White);
spriteBatch.End();
}
的主要區別在於,通過使用矩形你可以擴展你的紋理,以適應目標矩形的寬度和高度。
您可以在MSDN找到更多關於SpriteBatch.Draw方法的信息。
相關問題
- 1. 如何從Texture2D XNA c#獲取顏色和座標(x,y)?
- 2. 將Texture2D分配到現有的Texture2D
- 3. SceneBuilder座標與JavaFX座標
- 4. 座標由地理座標向球形座標的轉換
- 5. 帶座標和橫座標的座標系
- 6. 將座標調查座標轉換爲有效的esri座標
- 7. 從多個Texture2D生成一個Texture2D
- 8. 對齊座標原點的座標軸
- 9. 解析圖像座標上的座標
- 10. 作物的Texture2D Spritesheet
- 11. XNA:嘲諷的Texture2D
- 12. 鼠標座標
- 13. 座標轉換困境,用戶座標到設備座標?
- 14. 對數極座標(或對數極座標)VS極座標
- 15. 座標
- 16. 座標
- 17. XNA Texture2D Dispose()+ ObjectDisposedException
- 18. Texture2D iPhone SDK openGL
- 19. C#Texture2D SetData ARGB?
- 20. Texture2D到前
- 21. Texture2D SetData錯誤
- 22. directX 11 scale texture2D
- 23. Texture2D變黑
- 24. OpenGL座標映射到設備座標
- 25. 照片座標比。世界座標
- 26. 模擬座標點擊座標js/jQuery
- 27. respec座標在resthon respec座標
- 28. 從地圖座標過濾adsense座標
- 29. 循環座標,添加新座標
- 30. OpenGL座標匹配屏幕座標?
Texture2D只是一個紋理,它在遊戲中沒有位置。您必須創建一個類或結構來處理位置和紋理。 – Cyral 2013-04-27 15:08:44