XNA-第一次碰撞后球检测碰撞错误

本文关键字:碰撞 检测 错误 第一次 XNA- | 更新日期: 2023-09-27 18:25:21

在我的乒乓球游戏中,我基本上有两个球拍,一个设置为从所有墙壁/球拍上反弹的球,它过去工作得很好,但现在在第一次用球拍击球后,它开始检测到碰撞错误,你能看看这个代码吗?

Ball.cs:

public class Ball
{
    GreenPaddle gPaddle;
    BluePaddle bPaddle;
    public Texture2D ballTexture;
    public Vector2 ballPosition;
    public Rectangle ballRect;
    public float speed = 1f;
    bool movingUp, movingLeft;
    public Ball(GreenPaddle paddle, BluePaddle paddleb)
    {
        this.gPaddle = paddle;
        this.bPaddle = paddleb;
        movingLeft = true;
        movingUp = true;
    }
    public void LoadContent(ContentManager Content)
    {
        ballTexture = Content.Load<Texture2D>("ball");
        ballPosition = new Vector2(380, 225);
        ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y,
            20, 20);
    }
    public void Update(GameTime gameTime)
    {
        BallMovement();
        CollideWithPaddles();
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(ballTexture, ballRect, Color.White);
    }
    public void BallMovement()
    {
        if (movingUp) { ballPosition.Y -= speed; ballRect.Y -= (int)speed; }
        if (!movingUp) { ballPosition.Y += speed; ballRect.Y += (int)speed; }
        if (movingLeft) { ballPosition.X -= speed; ballRect.X -= (int)speed; }
        if (!movingLeft) { ballPosition.X += speed; ballRect.X += (int)speed; }
        if (ballPosition.Y < 83)
        {
            movingUp = false;
        }
        if (ballPosition.Y >= 500)
        {
            movingUp = true;
        }
    }

    public void CollideWithPaddles()
        {
            if (ballRect.Intersects(gPaddle.gpRect))
                movingLeft = false;     
            if (ballRect.Intersects(bPaddle.bpRect))
                movingLeft = true;
        }
    }

在我的Game1.cs中,我有一个部分,我可能认为它会引起一些问题:

public bool BallHitEffect()
    {
        //Plays an effect/animation when ball hits the paddle
        if (gPaddle.gpRect.Intersects(ball.ballRect) || bPaddle.bpRect.Intersects(ball.ballRect))
        {
            ball.speed += 0.5f;//Makes the ball go faster every paddle-hit.
            return true;
        }
        else { return false; }
    }

在我的Game1.cs更新方法中,我有这样的动画:

//If it is not already playing and there is collision, start playing
        if (!IsPlaying && BallHitEffect())
            IsPlaying = true;
        //Increment the frameTimePlayed by the time (in milliseconds) since the last frame
        if (IsPlaying)
            frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
        //If playing and we have not exceeded the time limit
        if (IsPlaying && frameTimePlayed < animationTime)
        {
            animatedSprite.Update(gameTime);
            // And increment your frames (Using division to figure out how many frames per second)
        }
        //If exceeded time, reset variables and stop playing
        else if (IsPlaying && frameTimePlayed >= animationTime)
        {
            frameTimePlayed = 0;
            IsPlaying = false;
            // TODO: Possibly custom animation.Stop(), depending on your animation class
        }

动画为800x400,每个图像/平铺为200x200。

XNA-第一次碰撞后球检测碰撞错误

这里的问题是,当球碰撞时,速度会增加0.5。然而,当你移动命中框时,你会绕过速度。这意味着命中框每帧移动1,但球移动0.5