0

私の卓球ゲームでは、基本的に 2 つのパドルがあります。ボールはすべての壁/パドルから跳ね返るように設定されています。以前は正常に機能していましたが、パドルで最初にヒットした後、衝突を間違って検出し始めました。このコードを見て、何が問題なのかを確認してください。

ボール.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 Update メソッドでは、アニメーション用にこれを持っています:

//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 です。

4

1 に答える 1

0

ここでの問題は、ボールが衝突すると速度が 0.5 増加することです。ただし、ヒットボックスを移動すると、速度が丸められます。つまり、当たり判定はフレームごとに 1 ずつ移動しますが、ボールは 0.5 ずつ移動します。

于 2013-07-29T22:20:39.727 に答える