我最近重新编写了我的代码以允许多个屏幕,现在我的代码来自拖动我的太空船(拖船)似乎不再起作用了。
我发布了我的整个更新方法,他们在“拖船”区域之外的某个地方出了问题
我一直盯着这个问题几个小时无济于事,我变得非常沮丧......
我会喜欢一些帮助。
#region Update
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
#region New Code
TouchCollection touchStart = TouchPanel.GetState();
switch (CurrentGameState)
{
case GameState.StartScreen:
//TouchCollection touchStart = TouchPanel.GetState();
if (btnPlay.Tapped == true) CurrentGameState = GameState.Playing;
btnPlay.Update(touchStart);
break;
case GameState.Playing:
#region Asteroid Rectangles
//Asteroid Rectangles
asteroid1Rect = new Rectangle((int)asteroid1Position.X - 20,
(int)asteroid1Position.Y - 20, 40, 40);
asteroid2Rect = new Rectangle((int)asteroid2Position.X - 20,
(int)asteroid2Position.Y - 20, 40, 40);
asteroid3Rect = new Rectangle((int)asteroid3Position.X - 20,
(int)asteroid3Position.Y - 20, 40, 40);
asteroid4Rect = new Rectangle((int)asteroid4Position.X - 20,
(int)asteroid4Position.Y - 20, 80, 40);
asteroid5Rect = new Rectangle((int)asteroid5Position.X - 20,
(int)asteroid5Position.Y - 20, 40, 40);
#endregion
//rotate
// The time since Update was called last.
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
//Rotation
RotationAngle += elapsed;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;
Rectangle punktRect = new Rectangle((int)punktPosition.X, (int)punktPosition.Y, 50, 50);
#region Ship-Punkt Collision
//Ship-Punkt Collisions
if (shipRect.Intersects(punktRect))
{
Score = Score + 1;
xSpeed = xSpeed + 20;
ySpeed = ySpeed + 20;
var rand = randomBleep.Next(0, 2);
SoundEffects[rand].Play();
punktPosition = new Vector2(randomX.Next(0, graphics.PreferredBackBufferWidth - 50), randomY.Next(0, graphics.PreferredBackBufferHeight - 50));
}
#endregion
#region ship-asteroid intersection
if (shipRect.Intersects(asteroid1Rect))
{
asteroid1Position = new Vector2(200, 300);
asteroid2Position = new Vector2(150, 200);
asteroid3Position = new Vector2(350, 200);
asteroid4Position = new Vector2(550, 200);
asteroid5Position = new Vector2(500, 100);
Vector2 asteroid1Speedini = new Vector2(-5, 0);
Vector2 asteroid2Speedini = new Vector2(0, -5);
Vector2 asteroid3Speedini = new Vector2(0, 5);
Vector2 asteroid4Speedini = new Vector2(0, -5);
Vector2 asteroid5Speedini = new Vector2(-5, 0);
shipPosition = new Vector2(10, 400);
Score = 0;
}
if (shipRect.Intersects(asteroid2Rect))
{
asteroid1Position = new Vector2(200, 300);
asteroid2Position = new Vector2(150, 200);
asteroid3Position = new Vector2(350, 200);
asteroid4Position = new Vector2(550, 200);
asteroid5Position = new Vector2(500, 100);
Vector2 asteroid1Speedini = new Vector2(-5, 0);
Vector2 asteroid2Speedini = new Vector2(0, -5);
Vector2 asteroid3Speedini = new Vector2(0, 5);
Vector2 asteroid4Speedini = new Vector2(0, -5);
Vector2 asteroid5Speedini = new Vector2(-5, 0);
shipPosition = new Vector2(10, 400);
Score = 0;
}
if (shipRect.Intersects(asteroid3Rect))
{
asteroid1Position = new Vector2(200, 300);
asteroid2Position = new Vector2(150, 200);
asteroid3Position = new Vector2(350, 200);
asteroid4Position = new Vector2(550, 200);
asteroid5Position = new Vector2(500, 100);
Vector2 asteroid1Speedini = new Vector2(-5, 0);
Vector2 asteroid2Speedini = new Vector2(0, -5);
Vector2 asteroid3Speedini = new Vector2(0, 5);
Vector2 asteroid4Speedini = new Vector2(0, -5);
Vector2 asteroid5Speedini = new Vector2(-5, 0);
shipPosition = new Vector2(10, 400);
Score = 0;
}
if (shipRect.Intersects(asteroid4Rect))
{
asteroid1Position = new Vector2(200, 300);
asteroid2Position = new Vector2(150, 200);
asteroid3Position = new Vector2(350, 200);
asteroid4Position = new Vector2(550, 200);
asteroid5Position = new Vector2(500, 100);
Vector2 asteroid1Speedini = new Vector2(-5, 0);
Vector2 asteroid2Speedini = new Vector2(0, -5);
Vector2 asteroid3Speedini = new Vector2(0, 5);
Vector2 asteroid4Speedini = new Vector2(0, -5);
Vector2 asteroid5Speedini = new Vector2(-5, 0);
shipPosition = new Vector2(10, 400);
Score = 0;
}
if (shipRect.Intersects(asteroid5Rect))
{
asteroid1Position = new Vector2(200, 300);
asteroid2Position = new Vector2(150, 200);
asteroid3Position = new Vector2(350, 200);
asteroid4Position = new Vector2(550, 200);
asteroid5Position = new Vector2(500, 100);
Vector2 asteroid1Speedini = new Vector2(-5, 0);
Vector2 asteroid2Speedini = new Vector2(0, -5);
Vector2 asteroid3Speedini = new Vector2(0, 5);
Vector2 asteroid4Speedini = new Vector2(0, -5);
Vector2 asteroid5Speedini = new Vector2(-5, 0);
shipPosition = new Vector2(10, 400);
Score = 0;
}
#endregion
//move Females
//asteroid1Position += female1Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
//asteroid2Position += female2Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
//asteroid3Position += female3Speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
asteroid1Position += asteroid1Speed;
asteroid2Position += asteroid2Speed;
asteroid3Position += asteroid3Speed;
asteroid4Position += asteroid4Speed;
asteroid5Position += asteroid5Speed;
#region boundaries
//Boundaries
int maxX = GraphicsDevice.Viewport.Width - asteroid1Rect.Width;
int maxY = GraphicsDevice.Viewport.Height - asteroid1Rect.Height;
int maxX2 = GraphicsDevice.Viewport.Width - asteroid2Rect.Width;
int maxY2 = GraphicsDevice.Viewport.Height - asteroid2Rect.Height;
int maxX3 = GraphicsDevice.Viewport.Width - asteroid3Rect.Width;
int maxY3 = GraphicsDevice.Viewport.Height - asteroid3Rect.Height;
int maxX4 = GraphicsDevice.Viewport.Width - asteroid4Rect.Width;
int maxY4 = GraphicsDevice.Viewport.Height - asteroid4Rect.Height;
int maxX5 = GraphicsDevice.Viewport.Width - asteroid5Rect.Width;
int maxY5 = GraphicsDevice.Viewport.Height - asteroid5Rect.Height;
#endregion
#region Check for Bounce
//Check for Bounce
if (asteroid1Position.X > maxX || asteroid1Position.X < 0)
asteroid1Speed.X *= -1;
if (asteroid1Position.Y > maxY || asteroid1Position.Y < 0)
asteroid1Speed.Y *= -1;
if (asteroid2Position.X > maxX || asteroid2Position.X < 0)
asteroid2Speed.X *= -1;
if (asteroid2Position.Y > maxY || asteroid2Position.Y < 0)
asteroid2Speed.Y *= -1;
if (asteroid3Position.X > maxX || asteroid3Position.X < 0)
asteroid3Speed.X *= -1;
if (asteroid3Position.Y > maxY || asteroid3Position.Y < 0)
asteroid3Speed.Y *= -1;
if (asteroid4Position.X > maxX || asteroid4Position.X < 0)
asteroid4Speed.X *= -1;
if (asteroid4Position.Y > maxY || asteroid4Position.Y < 0)
asteroid4Speed.Y *= -1;
if (asteroid5Position.X > maxX || asteroid5Position.X < asteroid5Rect.Width / 2)
asteroid5Speed.X *= -1;
if (asteroid5Position.Y > maxY + asteroid5Rect.Height / 2 || asteroid5Position.Y < asteroid5Rect.Height / 2)
asteroid5Speed.Y *= -1;
#endregion
#region Drag Ship
//Drag Ship
TouchCollection touchLocations = TouchPanel.GetState();
foreach (TouchLocation touchLocation in touchLocations)
{
Rectangle touchRect = new Rectangle
((int)touchLocation.Position.X, (int)touchLocation.Position.Y, shipRect.Width, shipRect.Height);
if (touchLocation.State == TouchLocationState.Pressed
&& shipRect.Intersects(touchRect))
{
shipPressed = true;
}
else if (touchLocation.State == TouchLocationState.Moved && shipPressed)
{
shipRect.X = touchRect.X - touchRect.Width / 2;
shipRect.Y = touchRect.Y - touchRect.Height / 2;
}
else if (touchLocation.State == TouchLocationState.Released)
{
shipPressed = false;
}
}
base.Update(gameTime);
break;
#endregion
}
//base.Update(gameTime);
#endregion
}
#endregion