我正在构建一个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
发生碰撞时,背景颜色不会改变。
答案 0 :(得分:1)
GraphicsDevice.Clear(Color.SlateGray);
用于Draw
函数。尝试创建新的Color
变量,并在Update
方法中更改该变量,并在使用GraphicsDevice.Clear(name of the variable);
时,在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*
}