在XNA中无法绘制非正方形图片

8
我正在制作一个乒乓球游戏。在调试期间,我一直使用44 x 44像素的红色正方形.png作为我的球。这个游戏使用这个正方形是可以正常工作的。
当我尝试用除了正方形以外的任何其他纹理替换它时,我就看不到屏幕上绘制出来的球,而且我也无法找出原因。我在Photoshop中制作的图像大小完全相同,使用的格式要么是.PNG,要么是.JPG,但结果都一样。请问您认为可能是什么原因导致这个问题?
下面是我球的代码,我的球的更新和绘制方法由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个回答

3

我注意到两件事情,我认为你在调用时不想将矩形尺寸减半。

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

此外,您正在将此“空白”精灵绘制在球的顶部,以便如果它们重叠,您就看不到球了,但如果是这种情况,我就不知道为什么正方形可以工作。

抱歉,我将纹理切成两半,只是为了缩小它以进行测试。我猜我忘记删除那一行了。我只在球和球拍碰撞的位置绘制空白精灵(实际上只是一个涂有Color.LightCoral颜色的白色块)。因此,一旦它们碰撞,它就会在那里绘制“空白”纹理,然后在下一个球拍碰到时移除它。 - Dave Voyles

2

如果您不想绘制整张图片,那么在绘制时请勿将任何内容作为SourceRect参数传递。

现在的设置是,在尝试绘制球时将“ballRect”作为SourceRect参数传递,并根据球的位置更新该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);

然后你可以在绘制调用中使用它:
spriteBatch.Draw(texture, ballPosition, sourceRect, Color.White,
                          RotationAngle, origin, .0f, SpriteEffects.None, 0);

编辑:您还将 .0f 作为“scale”参数传递,因此当它绘制您的球时,它的大小将为 0 像素,我猜这不是预期的行为。使用 1f 作为比例尺寸将以默认大小绘制它。


就是这样!我需要将SourceRect更改为null,并将比例更改为1。我不知道为什么它是0,但我猜是因为我的原始纹理只是一个大的红色块,所以它有多大并不重要。 - Dave Voyles

2
你正在将ballRect作为源矩形传递到Draw()中,它指定了精灵在纹理上的位置。如果你想使用整个纹理,请指定null。否则,请确保ballRect的值始终落在纹理内。
你似乎正在使用它来跟踪精灵在屏幕上的位置。从你的UpdatePosition函数来看:
ballRect.X = (int)ballPosition.X;
ballRect.Y = (int)ballPosition.Y;

这将会产生超出纹理范围的纹理坐标,这些坐标会被采样器夹紧。如果纹理完全由红色像素组成,则看起来可以正常工作,因为纹理边缘周围的像素都是红色的。但在带有透明边框的纹理中,精灵将被夹紧到该透明颜色。


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图像。它们很小并且加载非常快;此外,它们支持透明背景图像。


我已将原点重新定位到resetballpos线上方,但仍然看不到这个球。通常我一直使用PNG格式,但在这种情况下,我想尝试JPG格式,以验证这是否与透明度有关。 - Dave Voyles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接