简体   繁体   English

碰撞后精灵不在屏幕上

[英]sprites go off-screen after Collision

When the game starts the ball will bounce off the edges perfectly using this code: 当游戏开始时,使用以下代码,球将完美地反弹到边缘:

    if (Is_hit == true && ballFired == true )
    {
        //collision here
        ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
        ballXDir = +1;

        ballYDir = -1;
    }

    if (Ballvisible == true)
    {


        if (Keyboard.GetState().IsKeyDown(Keys.Space) && ballFired == false)
        {
            ballFired = true;

        }
        if (ballFired == true)
        {


            ballXPos = ballXPos - (ballSpeed * ballXDir);

            ballYPos = ballYPos + (ballSpeed * ballYDir);

        }
        if (ballXPos < 0)
        {
            ballXPos = -ballXPos;
            ballXDir = -1;


        }
        if (ballXPos > 960 - ballimg.Width)
        {
            ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
            ballXDir = +1;


        }
        if (ballYPos < 0)
        {
            ballYPos = +ballYPos;
            ballYDir = +1;

        }
      if (ballYPos > 700 - ballimg.Height)
      {
          Ballvisible = false;
      }


  }
  if (Ballvisible == false)
  {

      ballSpeed = 3;
      ballXDir = -1;
      ballYDir = -1;

      ballXPos = 450;
      ballYPos = 590;
      ballFired = false;
      Ballvisible = true;
  }



}

but once a collision has occurred the ball will bounce off another sprite(the player paddle) successfully but then the program seems to ignore this code and the ball will go straight off the screen. 但是一旦发生碰撞,球将成功地从另一个子画面(玩家桨)弹起,但是程序似乎忽略了此代码,球将直接离开屏幕。

collision code: 碰撞代码:

  // paddle collison detection 
            if   
   (  gameball.PositionRectangle_ball.Intersects(paddle.PositionRectangle_paddle))
            {

     collision_paddle = true; 
                gameball.CheckCollision(collision_paddle); // passes the collision bool value 
            }

It looks like you forgot to clear the Is_hit flag. 您好像忘了清除Is_hit标志。

If you don't need to check it anywhere else you can clear it in the first block of the posted code: 如果您不需要在其他任何地方进行检查,则可以在已发布代码的第一部分中将其清除:

if (Is_hit == true && ballFired == true )
{
    //collision here
    ballXPos = 960 - ballimg.Width - (ballXPos - 960 + ballimg.Width);
    ballXDir = +1;

    ballYDir = -1;
    Is_hit = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM