【问题标题】:How to properly stretch image in Monogame screen如何在 Monogame 屏幕中正确拉伸图像
【发布时间】:2019-11-26 20:03:03
【问题描述】:

我是 Monogame 和 C# 的初学者。我正在制作我的第一个游戏。 我在 Game1 构造函数中更改了屏幕尺寸。我正在绘制一个精灵,其中我将矩形宽度和高度设置为屏幕的宽度和高度以适合屏幕。我正在创建一个滚动背景,我必须更改精灵的位置。我将位置设置为矢量。零使精灵向右伸展。

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);
        }

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

【问题讨论】:

    标签: monogame


    【解决方案1】:

    您需要为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 的宽度/高度值乘以该数字。

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多