class の幅と高さを取得できることは知ってTexture2d
いますが、x 座標と y 座標を取得できないのはなぜですか? それらまたは何かのために別の変数を作成する必要がありますか? 大変な作業のようです。
2 に答える
Vector2
-object と関連付けて -object を使用する必要がありますTexture2D
。オブジェクト自体にTexture2D
は座標がありません。
テクスチャを描画する場合は、描画するのに が必要ですがSpriteBatch
、これはVector2D
座標を決定するのに が必要です。
public void Draw (
Texture2D texture,
Vector2 position,
Color color
)
これはMSDNから取得されます。
したがって、作成するか、struct
struct VecTex{
Vector2 Vec;
Texture2D Tex;
}
またはさらに処理が必要な場合はクラス。
A Texture2D object alone doesn't have any screen x and y coordinates.
In order to draw a texture on the screen, you must either set it's position by using a Vector2 or a Rectangle.
Here's an example using 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();
}
And here's an example using Rectangle:
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();
}
The main difference is that by using a Rectangle you are able to scale your texture to fit the destination rectangle's width and height.
You can find some more information about the SpriteBatch.Draw method at MSDN.