如何在Monogame屏幕中正确拉伸图像

问题描述 投票:0回答:1

我是Monogame和C#的初学者。我正在做我的第一场比赛。我在Game1构造函数中更改了屏幕尺寸。我正在绘制一个精灵,在其中将“矩形”的宽度和高度设置为屏幕的宽度和高度以使其适合屏幕。我正在创建一个滚动背景,必须为此更改精灵的位置。我将位置设置为Vector。零,使子画面向右伸展。

Game1
//member variables
        Vector2 stage;
        Texture2D fbTexture;
        private List<ScrollingBackground> sb;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            //change the screen size
            graphics.PreferredBackBufferHeight = 900;
            graphics.PreferredBackBufferWidth = 600;
            Window.Title = "Flappy Bird Returns";
            graphics.ApplyChanges();
        }

        protected override void LoadContent()
        {
            stage = new Vector2(graphics.PreferredBackBufferWidth, 
                    graphics.PreferredBackBufferHeight);

            //Day Background
            Texture2D dayTex = Content.Load<Texture2D>("Images/background-day");
            Vector2 daySpeed = new Vector2(1, 0);
            sb = new List<ScrollingBackground>()
            {
                //set the width and height to be the same as the screen 
                //dimensions then this will 
                //stretch the image past its original dimensions to fit.
                new ScrollingBackground(this, 
                spriteBatch, 
                dayTex,
                new Vector2(0,0), 
                new Rectangle(0,0, (int)stage.X, (int)stage.Y),
                daySpeed)

            };
            foreach (ScrollingBackground scrollbackg in sb)
                this.Components.Add(scrollbackg);
          }

//ScrollingBackground.cs
private SpriteBatch spritebatch;
        private Texture2D tex;
        private Vector2 position1;
        private Vector2 speed;
        private Rectangle srcRect;

        public ScrollingBackground(Game game, SpriteBatch spriteBatch, Texture2D tex, Vector2 pos, Rectangle srcRect, Vector2 speed) : base(game)
        {
            this.spritebatch = spriteBatch;
            this.tex = tex;
            this.position1 = pos;
            this.speed = speed;
            this.srcRect = srcRect;
        }

        public override void Draw(GameTime gameTime)
        {
            spritebatch.Begin();
            spritebatch.Draw(tex, position1, srcRect, Color.White);
            spritebatch.End();
            base.Draw(gameTime);
        }

With position1 in Draw

//Without position1
spritebatch.Draw(tex, srcRect, Color.White);

Without position1 in Draw

monogame
1个回答
0
投票

您需要给float scale参数指定一个参数。这意味着您需要为此方法使用更具体的签名:

SpriteBatch.Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, **float scale**, SpriteEffects effects, float layerDepth)

这意味着您将需要为不需要的所有其他参数指定一些默认值。例如,这就是使用上述方法签名编写相同Draw()方法的方法:

public override void Draw(GameTime gameTime)
{
    spritebatch.Begin();
    spritebatch.Draw(tex, position1, srcRect, Color.White, 0f, Vector2.Zero, 2f, SpriteEffects.None, 0f);
    spritebatch.End();
    base.Draw(gameTime);
}

这里,我将您的纹理放大到原始大小的2倍。将该2f更改为其他尺寸。请记住,它是使用srcRect的宽度/高度值乘以该数字。

© www.soinside.com 2019 - 2024. All rights reserved.