Reputation: 375
I'm building a 2D platformer and I want to have different colour backgrounds for each level. I've made an object that when collided with, it places the character onto the next level by changing the player.Position
, like so...
protected override void Update(GameTime gameTime){
if (player.Bounds.Intersects(teleportObj.Bounds))
{
GraphicsDevice.Clear(Color.SlateGray); // fails to change bg color
player.Position = new Vector2(172, 0); // successfully changes character position
MediaPlayer.Play(dungeonSong); // successfully plays new song
MediaPlayer.IsRepeating = true; // successfully repeats new song
}
}
I have already set a background for the first level to begin with in the Game1's Draw()
function like this:
GraphicsDevice.Clear(Color.CornflowerBlue);
But when my player collides with teleportObj
, the background color doesn't change.
Upvotes: 0
Views: 2943
Reputation: 657
GraphicsDevice.Clear(Color.SlateGray);
is used in Draw
function. Try creating new Color
variable, and change that variable in Update
method, and when using GraphicsDevice.Clear(name of the variable);
, use it in Draw function.
Code for that would look like this:
Color backgroundColor = Color.CornflowerBlue;
protected override void Update(GameTime gameTime)
{
if (player.Bounds.Intersects(teleportObj.Bounds))
{
backgroundColor = Color.SlateGray;
player.Position = new Vector2(172, 0);
MediaPlayer.Play(dungeonSong);
MediaPlayer.IsRepeating = true;
}
else backgroundColor = Color.CornflowerBlue;
}
protected override void Draw(SpriteBatch spriteBatch)
{
GraphicsDevice.Clear(backgroundColor);
*draw other stuff*
}
Upvotes: 1