簡體   English   中英

如何使用Spritebatch.Draw繪制RectangleF?

[英]How Would You Draw a RectangleF with Spritebatch.Draw?

我正在嘗試創建一個自上而下的游戲,可以在8個方向上移動。 我已經實現了Vector2以便以規范的方式移動。 我使用spritebatch系統繪制標准矩形(源矩形和目標矩形),但很快更改為RectangleF,因為常規矩形使用整數。

Spritebatch無法識別RectangleF,因此返回錯誤“無法從'System.Drawing.RectangleF'轉換為'Microsoft.Xna.Framework.Vector2'?”。 Color也返回歧義,無論是“ System.Drawing”還是“ Microsoft.Xna.Framework.Color”,它始終返回無法將其轉換為“ Microsoft.Xna.Framework.Rectangle”的方式。

在此代碼中,Vector2(位置)被調用並用於繪制RectangleF的位置。 然后,我使用Spritebatch.Draw繪制帶有Source和Destination的紋理,並且始終對其進行更新以暗示移動角色。

public static RectangleF destinationRectangle, sourceRectangle;

public void Draw(SpriteBatch spriteBatch)   
    {
        int width = 64;
        int height = 64;
        realPositionX = PlayerMovement.Position.X; 
        realPositionY = PlayerMovement.Position.Y;

        sourceRectangle = new RectangleF(currentFrame * width, row * height, width, height);
        destinationRectangle = new RectangleF(realPositionX, realPositionY, width, height);
        spriteBatch.Begin();
        spriteBatch.Draw(Player, destinationRectangle, sourceRectangle, Microsoft.Xna.Framework.Color.White); //Error Occurs Here
        spriteBatch.End();
    }

我很困惑如何解決此問題,是否有一種方法可以通過SpriteBatch繪制RectangleF,還是必須查看另一種方法?

謝謝。

絕對沒有理由使用RectangleF。 通常,您在游戲中使用基於像素的坐標系,因此Rectangle Class是您想要的。

除此之外,這是將玩家位置存儲在Vector2中並使用適當的重載的更好方法:

SpriteBatch.Draw (Texture2D, Vector2, Color)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM