당신은 개체를 좌표를 표현하기 위해 Vector2
및 Rectangle
을 모두 사용할 수 있습니다. 나는 보통 이런 식으로 작업을 수행합니다
public class GameObject
{
Texture2D _texture;
public Vector2 Position { get; set; }
public int Width { get; private set; } //doesn't have to be private
public int Height { get; private set; } //but it's nicer when it doesn't change :)
public Rectangle PositionRectangle
{
get
{
return new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
}
}
public GameObject(Texture2D texture)
{
this._texture = texture;
this.Width = texture.Width;
this.Height = texture.Height;
}
}
은 새로운 값으로 자신의 Position
속성을 설정 객체를 이동합니다. 이 값이 Position
에 직접적으로 의존의로
_player.Position = new Vector2(_player.Position.X, 100);
당신은 사각형에 대해 걱정할 필요가 없습니다.
내 게임 객체
또한 일반적으로 단지 충돌
또한 부를 수
//_player and _enemy are of type GameObject (or one that inherits it)
if(_player.PositionRectangle.Intersects(_enemy.PositionRectangle))
{
_player.Lives--;
_player.InvurnerabilityPeriod = 2000;
//or something along these lines;
}
을 테스트하기 위해 PositionRectangle
를 사용할 수 귀하의 Game.Update()
에
public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
{
spriteBatch.Draw(this._texture, this.Position, Color.White);
}
충돌 감지 코드로 자신을 그리는 방법을 포함 spriteBatch.Draw()
을 PositionRectangle
으로 설정하면 많은 차이를 느끼지 않을 것입니다.
는 항상 소수를 사용합니다. 또는 속도가 0.1f 일 때 int에 계속 추가하면 위치는 동일하게 유지됩니다. 하지만 플로트를 사용하면 자신의 입장이 계속 높아질 것입니다. –
@patryk - Vector2는 단순히 화면 픽셀 위치 외에 많은 다른 것들에 사용됩니다. 다른 용도의 대부분은 정수보다 정밀한 정밀도가 요구되는 산술을 포함합니다. –