8

卓球ゲームを作っています。デバッグ中は、単純な 44 x 44 の赤い四角形の .png をボールとして使用してきました。ゲームはこの正方形で正常に動作します。

テクスチャを正方形以外のものに置き換えようとすると、画面に描かれたボールが表示されず、その理由がわかりません。フォトショップで画像をまったく同じサイズにし、.PNGまたは.JPGのいずれかを使用して同じ結果を得ていますが、私の人生ではそれを理解できません。この問題の原因は何だと思いますか?

ボールのコードを下のテキストに残しました。私のボールの update メソッドと draw メソッドは、GameplayScreen (MS の GSM サンプルを使用) によって呼び出されています。

    public class Ball : IGameEntity
{
    #region Fields

    private Random rand;                // Random var
    private Texture2D texture;          // Texture for the ball
    private double direction;           // Directon the ball is traveling in                
    private bool isVisible;
    private bool hasHitLeftBat;         // Checked to see if the ball and bat have just collided
    private bool hasHitRightBat;        // Checked to see if the ball and bat have just collided
    private Vector2 ballPosition, resetBallPos, oldBallPos;
    private Rectangle ballRect;
    public float Speed;
    private SpriteBatch spriteBatch;    // Spritebatch
    private bool isBallStopped;
    private Vector2 origin;             // Locate the mid-point of the ball
    public float RotationAngle;
    private AIBat rightBat;             // Player's Bad
    private Bat leftBat;                // AI Bat
    private float ballRelativePos;
    private Rectangle rectangle3;       // Used to draw the collison rectangle
    private Texture2D blank;            // Texture to be drawn on the collision rectangle

    GameplayScreen gameplayScreen;      // Creates an instance of the GameplayScreen
    Game1 gameInstance;                 // Creates an instance of the Game1 class
    int selectedStage;                  // Pass this into GameplayScreen for selecting easy, medium, or hard


    #endregion

    #region Constructors and Destructors

    /// <summary>
    /// Constructor for the ball
    /// </summary>
    public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
    {
        Speed = 15f;
        texture = contentManager.Load<Texture2D>(@"gfx/balls/redBall");
        direction = 0;
        ballRect = new Rectangle(0, 0, texture.Width /2, texture.Height /2);
        resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);
        ballPosition = resetBallPos;
        rand = new Random();
        isVisible = true;
        origin = new Vector2(texture.Width / 2, texture.Height / 2);
        leftBat = bat; // Creates a new instance of leftBat so that I can access Position.X/Y for LeftBatPatcicles()
        rightBat = aiBat;// Creates a new instance of leftBat so that  can access Position.X/Y for RightBatPatcicles()
        gameplayScreen = new GameplayScreen(null, selectedStage);
        gameInstance = new Game1();
        Rectangle rectangle3 = new Rectangle();
        blank = contentManager.Load<Texture2D>(@"gfx/blank");               

        //   pes = new ParticleEmitterService(game);
    }

    public Ball(Bat myBat)
    {
        leftBat = myBat;          // this assigns and instantiates the member bat
                                  // with myBat which was passed from the constructor
    }

    #endregion

    #region Methods

    /// <summary>
    /// Draws the ball on the screen
    /// </summary>
    public void Draw(SpriteBatch spriteBatch)
    {
        if (isVisible)
        {
            // Draws the rotaing ball
            spriteBatch.Draw(texture, ballPosition, ballRect, Color.White,
                              RotationAngle, origin, .0f, SpriteEffects.None, 0);

            spriteBatch.Draw(blank, rectangle3, Color.LightCoral);
        }
    }

    /// <summary>
    /// Updates position of the ball. Used in Update() for GameplayScreen.
    /// </summary>
    public void UpdatePosition(GameTime gameTime)
    {
        ballRect.X = (int)ballPosition.X;
        ballRect.Y = (int)ballPosition.Y;
        oldBallPos.X = ballPosition.X;
        oldBallPos.Y = ballPosition.Y;

        ballPosition.X += Speed * ((float)Math.Cos(direction));

        ballPosition.Y += Speed * ((float)Math.Sin(direction));
        bool collided = CheckWallHit();


        // Stops the issue where ball was oscillating on the ceiling or floor
        if (collided)
        {
            ballPosition.X = oldBallPos.X + Speed * (float)1.5 * (float)Math.Cos(direction);
            ballPosition.Y = oldBallPos.Y + Speed * (float)Math.Sin(direction);
        }

        // As long as the ball is to the right of the back, check for an update
        if (ballPosition.X > leftBat.BatPosition.X)
        {
            // When the ball and bat collide, draw the rectangle where they intersect
            BatCollisionRectLeft();
        }

        // As longas the ball is to the left of the back, check for an update
        if (ballPosition.X < rightBat.BatPosition.X)
        {   // When the ball and bat collide, draw the rectangle where they intersec
            BatCollisionRectRight();
        }

        // The time since Update was called last.
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        // Rotation for the ball
        RotationAngle += elapsed;
        float circle = MathHelper.Pi * 2;
        RotationAngle = RotationAngle % circle;

        //      base.Update(gameTime);
        gameInstance.update();

    }


    /// <summary>
    /// Checks for the current direction of the ball
    /// </summary>
    public double GetDirection()
    {
        return direction;
    }

    /// <summary>
    /// Checks for the current position of the ball
    /// </summary>
    public Vector2 GetPosition()
    {
        return ballPosition;
    }

    /// <summary>
    /// Checks for the current size of the ball (for the powerups)
    /// </summary>
    public Rectangle GetSize()
    {
        return ballRect;
    }



    /// <summary>
    /// Checks to see if ball went out of bounds, and triggers warp sfx. Used in GameplayScreen.
    /// </summary>
    public void OutOfBounds()
    {
        AudioManager.Instance.PlaySoundEffect("Muzzle_shot");
    }

    /// <summary>
    /// Speed for the ball when Speedball powerup is activated
    /// </summary>
    public void PowerupSpeed()
    {
        Speed += 20.0f;
    }

    /// <summary>
    /// Check for where to reset the ball after each point is scored
    /// </summary>
    public void Reset(bool left)
    {
        if (left)
        {
            direction = 0;
        }
        else
        {
            direction = Math.PI;
        }

        ballPosition = resetBallPos; // Resets the ball to the center of the screen
        isVisible = true;
        Speed = 15f; // Returns the ball back to the default speed, in case the speedBall was active
        if (rand.Next(2) == 0)
        {
            direction += MathHelper.ToRadians(rand.Next(30));
        }
        else
        {
            direction -= MathHelper.ToRadians(rand.Next(30));
        }
    }

    /// <summary>
    /// Shrinks the ball when the ShrinkBall powerup is activated
    /// </summary>
    public void ShrinkBall()
    {
        ballRect = new Rectangle(0, 0, texture.Width / 2, texture.Height / 2);
    }

    /// <summary>
    /// Stops the ball each time it is reset. Ex: Between points / rounds
    /// </summary>
    public void Stop()
    {
        isVisible = true;
        Speed = 0;
        isBallStopped = true;
    }

    /// <summary>
    /// Checks for collision with the ceiling or floor. 2*Math.pi = 360 degrees
    /// </summary>
    private bool CheckWallHit()
    {
        while (direction > 2 * Math.PI)
        {
            direction -= 2 * Math.PI;
            return true;
        }

        while (direction < 0)
        {
            direction += 2 * Math.PI;
            return true;
        }

        if (ballPosition.Y <= 0 || (ballPosition.Y > resetBallPos.Y * 2 - ballRect.Height))
        {
            direction = 2 * Math.PI - direction;
            return true;
        }
        return true;
    }

    /// <summary>
    /// Used to determine the location where the particles will initialize when the ball and bat collide
    /// </summary>
    private void BatCollisionRectLeft()
    {
        // For the left bat
        if (ballRect.Intersects(leftBat.batRect))
        {
            rectangle3 = Rectangle.Intersect(ballRect, leftBat.batRect);
        }
    }

    /// <summary>
    ///Checks for collision of Right Bat
    /// </summary>
    private void BatCollisionRectRight()
    {
        // for the right bat
        if (ballRect.Intersects(rightBat.batRect))
        {
            rectangle3 = Rectangle.Intersect(ballRect, rightBat.batRect); ;
        }
    }
4

4 に答える 4

3

私が気付いている2つのこと、あなたが電話するときにあなたがあなたの長方形の次元を半分にしたくないと思う

ballRect = new Rectangle(0, 0, texture.Width /2, texture.Height /2);

また、このスプライトをボールの上に描画しているblankので、それらが重なっている場合はボールが見えませんが、その場合、なぜそれが正方形で機能したのかわかりません。

于 2012-08-08T17:08:38.360 に答える
2

画像全体を描画したくない場合を除いて、描画呼び出しでSourceRectパラメータとして何も渡さないでください。

現在の設定方法では、ボールを描画しようとするときにSourceRectとして「ballRect」を渡します。そのballRectパラメータはボールの位置に応じて更新されるため、画像の一部を描画しようとしています。これは、テクスチャのサイズをはるかに超えています。

ボール全体を描画する場合は、次を使用します。

spriteBatch.Draw(texture, ballPosition, null, Color.White,
                          RotationAngle, origin, .0f, SpriteEffects.None, 0);

ボールの左上の象限だけを描画したい場合は、SourceRectとして次の長方形を渡すことができます。

Rectangle sourceRect = new Rectangle(0, 0, texture.Width / 2, texture.Height / 2);

次に、それをDraw呼び出しで使用できます。

spriteBatch.Draw(texture, ballPosition, sourceRect, Color.White,
                          RotationAngle, origin, .0f, SpriteEffects.None, 0);

編集:「スケール」パラメータとして.0fも渡しているので、ボールを描画すると、サイズは0ピクセルになります。これは、意図した動作ではないと思います。スケールに1fを使用すると、デフォルトのサイズで描画されます。

于 2012-08-08T22:20:31.167 に答える
2

スプライトがテクスチャ上に存在する場所を指定するソース矩形として渡しballRectています。テクスチャ全体を使用したい場合は代わりに指定してください。それ以外の場合は、 の値が常にテクスチャ内にあることを確認してください。Draw()nullballRect

画面上のスプライトの位置を追跡するために使用しているようです。あなたのUpdatePosition機能から:

ballRect.X = (int)ballPosition.X;
ballRect.Y = (int)ballPosition.Y;

これは、サンプラーによってクランプされているテクスチャの境界の外側にテクスチャ座標を生成しています。完全に赤のピクセルで構成されるテクスチャでは、テクスチャのエッジの周囲のピクセルがすべて赤であるため、これは機能しているように見えます。透明な境界線を持つテクスチャでは、スプライトは透明な色に固定されます。

于 2012-08-08T20:36:46.650 に答える
0

私が気づいたのは、その行にあることです

resetBallPos = new Vector2(ScreenSize.X / 2 + origin.X, ScreenSize.Y / 2 + origin.Y);

実際に値を割り当てる前に、コンストラクター内で変数「origin」のプロパティにアクセスしています。ただし、この値の数行下に値を割り当てます。この行を移動します:

origin = new Vector2(texture.Width / 2, texture.Height / 2);

それ以上であること。これで問題は解決すると思います。赤いブロックから適切な画像への移行で、あなたは思った以上に変化したかもしれません。

また、補足として、XNA を使用する場合は、主に PNG 画像を使用するようにしてください。それらは小さく、非常に速くロードされます。さらに、透明な背景画像をサポートします。

于 2012-08-08T18:05:44.767 に答える