Monogame - 更改背景颜色错误

Monogame - Changing background colour error

我正在构建一个 2D 平台游戏,我希望每个关卡都有不同的颜色背景。我制作了一个对象,当与之发生碰撞时,它通过更改 player.Position 将角色放置到下一个级别,就像这样...

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

我已经在 Game1 的 Draw() 函数中设置了第一关的背景,如下所示:

GraphicsDevice.Clear(Color.CornflowerBlue);

但是当我的播放器与 teleportObj 发生碰撞时,背景颜色没有改变。

GraphicsDevice.Clear(Color.SlateGray);用于Draw函数。尝试创建新的 Color 变量,并在 Update 方法中更改该变量,并在使用 GraphicsDevice.Clear(<em> 变量名称 </em> );,在Draw函数中使用。

代码如下所示:

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*
}