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);
}
'GetPressedKeys'를 사용하면 포크로 눈을 찔러 내고 싶습니다. 키보드 상태 ('KeyboardState ks = Keyboard.GetState()')를 저장하고'if (ks.IsKeyDown (Keys.W)'등) 쿼리하십시오 .. 루프와 "else"를 제거하십시오 –
또한 '새로운 Vector2 (point.X, point.Y)'의 요점은 무엇입니까?'point'를 그냥 넘겨 주면됩니다. (아마도 가장 좋은 점은 ... "Point"라는 XNA 구조가 있습니다. 이것은 2D 정수입니다. 위치, 아마도 어쨌든 당신이하고있는 일에 더 적합 할 것입니다.) 어쨌든, 이제 저는 실제 질문에 답할 것입니다 ... –