Gap in parallaxing background loop
Posted
by
CinetiK
on Game Development
See other posts from Game Development
or by CinetiK
Published on 2013-06-28T20:26:53Z
Indexed on
2013/06/28
22:29 UTC
Read the original article
Hit count: 318
The bug here is that my background kind of offset a bit itself from where it should draw and so I have this line. I have some troubles understanding why I get this bug when I set a speed that is different then 1,2,4,8,16,...
In main class I set the speed depending on the player speed
bgSpeed = -(int)playerMoveSpeed.X / 10;
and here's my background class
class ParallaxingBackground
{
Texture2D texture;
Vector2[] positions;
public int Speed { get; set;}
public void Initialize(ContentManager content, String texturePath, int screenWidth, int speed)
{
texture = content.Load<Texture2D>(texturePath);
this.Speed = speed;
positions = new Vector2[screenWidth / texture.Width + 2];
for (int i = 0; i < positions.Length; i++)
{
positions[i] = new Vector2(i * texture.Width, 0);
}
}
public void Update() {
for (int i = 0; i < positions.Length; i++)
{
positions[i].X += Speed;
if (Speed <= 0)
{
if (positions[i].X <= -texture.Width)
{
positions[i].X = texture.Width * (positions.Length - 1);
}
}
else
{
if (positions[i].X >= texture.Width*(positions.Length - 1))
{
positions[i].X = -texture.Width;
}
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < positions.Length; i++)
{
spriteBatch.Draw(texture, positions[i], Color.White);
}
}
}
© Game Development or respective owner