【发布时间】:2012-04-03 17:39:41
【问题描述】:
我正在用 C# 制作 Space Invaders 游戏,使用 Bitmap 存储入侵者、炸弹等的图像。
让我困惑的是:
我有一个抽象类GameObject 代表游戏中的简单元素:
protected Bitmap image;
protected Rectangle bounds;
protected Rectangle movementBounds;
public GameObject(ref Bitmap image, Point position, Rectangle movementBounds)
{
this.image = image;
this.bounds = new Rectangle(position, new Size(image.Width, image.Height));
this.movementBounds = movementBounds;
}
比代表Shield 的单个正方形的ShieldSegment 类,它有四个不同阶段损坏的图像:
private int timesHit = 0;
private IList<Bitmap> alternateImages = new List<Bitmap>();
public ShieldSegment(ref List<Bitmap> images, Point position, Rectangle movementBounds)
: base(ref images.First(), position, movementBounds)
{
alternateImages = images;
}
还有一个Shield 类,它将整个盾牌表示为一个段列表:
private IList<ShieldSegment> segments = new List<ShieldSegment>();
//fill, upper left, upper right, lower left, lower right
public Shield(ref List<List<Bitmap>> images, Point position, Size imageSize)
{
//upper row
Point startPosition = position;
segments.Add(new ShieldSegment(ref images.ElementAt(1), startPosition,new Rectangle(startPosition, imageSize)));
startPosition.X += imageSize.Width;
segments.Add(new ShieldSegment(ref images.ElementAt(0), startPosition,new Rectangle(startPosition, imageSize)));
startPosition.X += imageSize.Width;
segments.Add(new ShieldSegment(ref images.ElementAt(0), startPosition, new Rectangle(startPosition, imageSize)));
startPosition.X += imageSize.Width;
segments.Add(new ShieldSegment(ref images.ElementAt(2), startPosition, new Rectangle(startPosition, imageSize)));
//middle row
startPosition = position;
startPosition.Y += imageSize.Height;
...
我需要将列表元素作为引用传递,因为(如果我错了,很抱歉)为每个段和盾牌重新存储图像会很浪费。将单个引用传递给Bitmap 时它工作正常,但在这里我得到a ref or out argument must be an assignable variable。
有什么办法吗?
【问题讨论】: