-1
작은 탱크와 검은 색 사각형이있는이 프로그램이 있습니다. 탱크는 사용자 입력에 응답하고 탱크에 닿으면 정지합니다. 그러나, 상자와 탱크를 모두 포함하는 창이 나타나면 사용자 입력이 없습니다. 화살표 키 중 하나를 누르면 탱크가 전혀 움직이지 않습니다.XNA 4.0 2d 충돌 감지
여기 내 코드에서 더 중요한 몇 가지 예만 있습니다.
public class TopDownShooter : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D tank, blackSquare;
private KeyboardState state;
private float angle1 = 0;
private float angle2 = 0;
private float xLocation, yLocation;
Vector2 location1 = new Vector2(900, 400), origin1;
Vector2 location2 = new Vector2(400, 400), origin2;
Rectangle playerRectangle, obstacleRectangle;
/// <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 (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
playerRectangle = Move(playerRectangle);
base.Update(gameTime);
}
private Boolean IsNotCollision(Rectangle rectangle1, Rectangle rectangle2)
{
if (rectangle1.Intersects(rectangle2)) return true;
else return false;
}
/// <summary>
/// Sets the velocity of the targeted object based on user keyboard input
/// </summary>
private Rectangle Move(Rectangle rectangle)
{
state = Keyboard.GetState();
xLocation = rectangle.X;
yLocation = rectangle.Y;
if (IsNotCollision(obstacleRectangle, playerRectangle))
{
if (state.IsKeyDown(Keys.Left)) rectangle.X += -4;
if (state.IsKeyDown(Keys.Right)) rectangle.X += 4;
if (state.IsKeyDown(Keys.Up)) rectangle.Y += -4;
if (state.IsKeyDown(Keys.Down)) rectangle.Y += 4;
}
return rectangle;
}
/// <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.CornflowerBlue);
spriteBatch.Begin();
playerRectangle = new Rectangle(0, 0, tank.Width, tank.Height);
origin1 = new Vector2(tank.Width/2, tank.Height);
obstacleRectangle = new Rectangle(0, 0, blackSquare.Width, blackSquare.Height);
origin2 = new Vector2(blackSquare.Width/2, blackSquare.Height);
spriteBatch.Draw(tank, location1, playerRectangle, Color.White, angle1, origin1, 1.0f, SpriteEffects.None, 1);
spriteBatch.Draw(blackSquare, location2, obstacleRectangle, Color.White, angle2, origin2, 1.0f, SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
이 질문에 대한 답을 제공하지 않습니다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남겨 둡니다. – VMAtm
나는 더 큰 대답을 가졌지 만 OP에서 실제 위치를 업데이트하지 않는다는 작은 감독처럼 보입니다. 그러므로, 이것은 제 대답입니다 .. –
그것은 옳은 대답입니다. Jonheel은 모든 프레임에서 동일한 위치에 탱크를 그립니다. – jlvaquero