我試圖以某種方式將紋理平鋪到矩形。例如,這裏是我的質地:將紋理平鋪到頂部的矩形
目前,它平鋪到我的矩形,使得紋理得到在矩形的頂部切斷:
我想以平鋪我的紋理,使其在我的矩形底部被切斷:
我用下面的邏輯來創建和我的比賽和瓷磚畫一個矩形它:
public class HoopsGame : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Body _ground;
Texture2D _groundTexture;
World _world;
public HoopsGame()
{
graphics = new GraphicsDeviceManager(this);
}
protected override void Initialize()
{
Content.RootDirectory = "Content";
_world = new World(new Vector2(0f, 9.8f));
int groundHeight = (int)(graphics.GraphicsDevice.Viewport.Height * 0.05);
Rectangle groundRectangle = new Rectangle(0, graphics.GraphicsDevice.Viewport.Height - groundHeight, graphics.GraphicsDevice.Viewport.Width, groundHeight);
_ground = BodyFactory.CreateRectangle(_world, groundRectangle.Width, groundRectangle.Height, 0f, groundRectangle);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
_groundTexture = Content.Load<Texture2D>("court");
}
protected override void UnloadContent()
{
Content.Unload();
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap, DepthStencilState.Default, RasterizerState.CullNone);
spriteBatch.Draw(_groundTexture, new Vector2(0, graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData).Height), (Rectangle)_ground.UserData, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
抵消將是巨大的,但我不知道如何在XNA中應用紋理偏移。 – Alexandru 2015-03-08 18:24:16
對不起,我不太熟悉XNA,但是當你寫: spriteBatch.Draw(_groundTexture,new Vector2(0,graphics.GraphicsDevice.Viewport.Height - ((Rectangle)_ground.UserData).Height),(矩形)_ground.UserData,Color.White); 是不是你分配一個紋理到你的矩形形式 SpriteBatch.Draw(Texture2D,RectangleCoordinates,Color)。 然後我假設你可以做類似於: SpriteBatch.Draw(Texture2D,(Rectangle_x,Rectangle_y - y_offset),Color) – 2015-03-09 00:40:45
不幸的是,似乎沒有任何東西。 'Vector2'只是翻譯而已。只有一個'Draw(Texture2D紋理,Rectangle destinationRectangle,Rectangle?sourceRectangle,Color color)'方法可以做類似的事情,但它仍然沒用。 sourceRectangle首先被繪製,然後被縮放到destinationRectangle。所以我需要確保源矩形均勻地適合我的'Texture2D',然後繪製到該矩形,但問題是我無法找到一種方法來抵消紋理到目前爲止。這似乎比它的價值更麻煩。 – Alexandru 2015-03-09 22:55:34