2014-12-17 1 views
1

화면 높이에 따라 이미지의 크기를 조정 하려는데 문제가 있습니다. 현재 문제는 단순히 이미지가 표시되지 않는다는 것입니다. 다음은 코드입니다.크기가 조정 된 스프라이트가 표시되지 않습니다.

class Board 
{ 
    private Texture2D texture; 
    private int screenWidth = Game1.Instance.GraphicsDevice.Viewport.Width; 
    private int screenHeight = Game1.Instance.GraphicsDevice.Viewport.Height; 
    private Vector2 location; 
    private Rectangle destination; 

    public Board(Texture2D texture) 
    { 
     this.texture = texture; 
     this.location = new Vector2(200, 0); 
     this.destination = new Rectangle((int)location.X, (int)location.Y, texture.Width * (screenHeight/texture.Height), screenHeight); 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Begin(); 
     spriteBatch.Draw(texture, 
         destination, 
         Color.White); 
     spriteBatch.End(); 
    } 
} 

이미지가 너무 넓기는하지만 이전에 표시되었으므로 주 루프 내의 코드는 정상입니다. 그래서 짧은 질문에 내 질문은 ...이 코드가 무엇이 잘못되어 더 좋은 방법이 있을까요?

+0

제목을 편집했습니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –

답변

1
texture.Width * (screenHeight/texture.Height) 

정수 나누기를 사용하고 있습니다. 텍스처가 화면보다 크면 0을 반환합니다. 너비가 0이면 텍스처가 표시되지 않습니다. 대신, double 또는 float에 하나의 피연산자 캐스트 :

texture.Width * (screenHeight/(double)texture.Height) 

이 부문/곱셈은 당신이 예상대로 작동 할 수 있도록하는 double를 반환합니다.

+0

그랬어! 고마워요! – Firearrow5235

+0

문제 없습니다. 특히 프레임 워크 클래스로 작업 할 때 정수 부분은 발견하기가 어려울 수 있습니다. – BradleyDotNET

관련 문제