2014-03-28 5 views
1

스프라이트의 왼쪽 상단 좌표를 가져 오는 데 문제가 있습니다. bounds.location.x \ y 또는 bounds.left \ top와 함께 texture2d.bounds를 사용하려고했습니다. 그러나 어떤 이유로이 매개 변수는 x = 0, y = 0이고 객체의 움직임은 변경되지 않습니다 (심지어 시작점이 0,0이 아닌 경우). 어떻게 해결할 수 있습니까?XNA 스프라이트의 왼쪽 상단 좌표를 얻습니다.

+0

이미 그려진 스프라이트? 그런 일은 없을 것이다. 스프라이트가 위치가있는 객체로 표시되지 않는 이유는 무엇입니까? – BradleyDotNET

답변

3

Texture2D.BoundsMSDN document에서 볼 수있는 것처럼 리소스의 크기를 가져옵니다. XY 위치는 항상 0이며, 필요하지 않으므로 Bounds의 유일한 이유는 텍스처의 WidthHeight입니다.

이렇게 말하면 Texture2D은 텍스처를 나타낼 뿐이며 이 아닌은 위치를 나타냅니다. 당신이해야 할 일은 2 차원 벡터 인 Vector2을 사용하여 스프라이트의 위치를 ​​정하는 것입니다.

예 : 각 스프라이트의 위치와 질감을 만드는

Texture2D sprite; 
Vector2 position; 
... 
protected override void Draw(GameTime gameTime) 
{ 
    spriteBatch.Begin(); 
    spriteBatch.Draw(sprite, position, Color.White) 
    spriteBatch.End(); 
} 

대신, 당신은 별도의 스프라이트 클래스를 사용하면 위치, 질감 및 도면을 처리 할 수 ​​있습니다.

public class Sprite 
{ 
    public Texture2D Texture { get; set; } 
    public Vector2 Position { get; set; } 

    public Sprite(Texture2D texture, Vector2 initialPosition) 
    { 
     Texture = texture; 
     Position = initialPosition; 
    } 
    public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime) 
    { 
     spriteBatch.Draw(Texture, Position, Color.White); 
    } 
} 
관련 문제