2011-02-13 1 views
1

어제 XNA에서 뷰포트를 사용하여 위치를 변경할 때 필자가 사용하는 스프라이트가 뷰포트보다 빠르게 이동하는 이유를 알 수 없었습니다. 다른 값 유형 (int 대 float)과 관련이있을 수 있다는 느낌이 들었지 만,이 점에 대해 정교하게 신경 쓰겠습니까?스프라이트 위치가 뷰포트보다 빠르게 이동합니다.

여기에 사용 된 코드 ... 모든

Viewport myViewport; 
    Texture2D t; 
    SpriteFont f; 
    Vector2 point = new Vector2(0, 0); 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 
     Keys[] pressedKeys = Keyboard.GetState().GetPressedKeys(); 
     for (int i = 0; i < pressedKeys.Length; i++) 
     { 
      if (pressedKeys[i] == Keys.W) 
      { 
       point.Y--; 
      } 
      else if (pressedKeys[i] == Keys.A) 
      { 
       point.X--; 
      } 
      else if (pressedKeys[i] == Keys.S) 
      { 
       point.Y++; 
      } 
      else if (pressedKeys[i] == Keys.D) 
      { 
       point.X++; 
      } 
     } 
     myViewport.X = (int)point.X; 
     myViewport.Y = (int)point.Y; 
     GraphicsDevice.Viewport = myViewport; 
     // TODO: Add your update logic here 

     base.Update(gameTime); 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.White); 
     spriteBatch.Begin(); 
     spriteBatch.Draw(t, new Vector2(point.X, point.Y), Color.White); 
     spriteBatch.End(); 
     // TODO: Add your drawing code here 

     base.Draw(gameTime); 
    } 
+0

'GetPressedKeys'를 사용하면 포크로 눈을 찔러 내고 싶습니다. 키보드 상태 ('KeyboardState ks = Keyboard.GetState()')를 저장하고'if (ks.IsKeyDown (Keys.W)'등) 쿼리하십시오 .. 루프와 "else"를 제거하십시오 –

+0

또한 '새로운 Vector2 (point.X, point.Y)'의 요점은 무엇입니까?'point'를 그냥 넘겨 주면됩니다. (아마도 가장 좋은 점은 ... "Point"라는 XNA 구조가 있습니다. 이것은 2D 정수입니다. 위치, 아마도 어쨌든 당신이하고있는 일에 더 적합 할 것입니다.) 어쨌든, 이제 저는 실제 질문에 답할 것입니다 ... –

답변

5

첫째, 당신은 아마 당신의 그리기 함수에서 뷰포트를 설정해야입니다. 둘째, 뷰포트 범위가 항상 화면에 남아 있도록해야합니다.

어쨌든 SpriteBatch의 좌표계가 클라이언트 공간의이라는 측면에서 이기 때문에 이동하는 이유는 다음과 같습니다.

즉, SpriteBatch에 따른 위치 (0,0)는 GraphicsDevice.Viewport의 왼쪽 위 모서리입니다.

이렇게하면 렌더링 위치를 수정하는 두 가지 다른 작업을 효과적으로 수행하기 때문에 스프라이트가 예상 속도의 두 배로 이동합니다.