【发布时间】:2017-03-16 10:33:10
【问题描述】:
我是编程新手和 c# 新手,但我正在尝试制作 2D 游戏。我创建了一个背景类和一个关闭类,关闭类用于我要实现的退出按钮。我想要实现的是,当我释放关闭按钮时,背景会逐渐降低,从白色到深白色。问题是我不知道如何真正编码。这是我的代码的视图。
关闭课程
private Texture2D texture;
private Vector2 position;
private Rectangle bounds;
private Color color;
MouseState oldstate;
public Close(Texture2D texture, Vector2 position){
this.texture = texture;
this.position = position;
bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
color = new Color(40, 40, 40);
}
public void Update(GameTime gameTime){
MouseState state = Mouse.GetState();
Point point = new Point(state.X, state.Y);
if(bounds.Contains(point) && !(state.LeftButton == ButtonState.Pressed)){
color = new Color(235, 50, 50);
} else if((!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Pressed)) || (!(bounds.Contains(point)) && (state.LeftButton == ButtonState.Released))){
color = new Color(40, 40, 40);
}
if((state.LeftButton == ButtonState.Pressed) && (oldstate.LeftButton == ButtonState.Released) && (bounds.Contains(point))){
color = new Color(172, 50, 50);
}
if((state.LeftButton == ButtonState.Released) && (oldstate.LeftButton == ButtonState.Pressed) && (bounds.Contains(point))){
}
oldstate = state;
}
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, position, color);
}
后台类
public Color color;
public Background(Color color){
this.color = color;
}
public void Update(GameTime gameTime){
}
更具体一点,我希望在 Background 类中改变颜色,并能够通过 Close 类调用它。另外,请记住,背景颜色是在 Game1 类中指定的,并且也从中调用了 Update 方法。
无论如何,我们将不胜感激。
【问题讨论】:
-
你能把
Background变成Static类吗? -
那种是可以的,但不会让背景慢慢变成它颜色的低调。