Reputation: 45
I am a beginner in Monogame and C#. I am making my first game. I have changed the screen dimensions in Game1 constructor. I am drawing a sprite where I have set the Rectangle width and height to width and height of the screen to fit it on the screen. I am creating a scrolling background for which I have to change the position of the sprite. I am setting the position to Vector. Zero which is making the sprite stretched towards the right.
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);
Upvotes: 1
Views: 2104
Reputation: 295
You need to give an argument to the float scale
parameter. This means you'll need to use a more specific signature for this method :
SpriteBatch.Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, **float scale**, SpriteEffects effects, float layerDepth)
At the same time, you'll need to specify default values for every other parameter. For example, here's how you'd write that same Draw() method with the aforementioned method signature :
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);
}
Here, I put your texture to 2x it's original size. Change that 2f
to the size you want. Keep in mind the final size will be the width/height values of your srcRect
multiplied by that number.
Upvotes: 3