這尚未經過測試,但你可以試試:
public bool CollidsWith(FrameworkElement sprite1, FrameworkElement sprite2, bool pixelPerfect)
{
try
{
Rect r1 = Bounds(sprite1);
Rect r2 = Bounds(sprite2);
if (r1.IntersectsWith(r2))
{
if (!pixelPerfect)
return true;
else
{
Point pt = new Poin();
for (int x = (int)r1.Left; x < (int)r1.Right; x++)
{
for (int y = (int)r1.Top; y <(int)r1.Bottom; y++)
{
pt.X = x;
pt.Y = y;
if (VisualTreeHelper.HitTest(sprite2, pt) != null)
return true;
}
}
return false;
}
else
return false;
}
}
catch { }
return false; // we should not get here
}
public Rect Bounds(FrameworkElement sprite)
{
get
{
Point ptBottomRight = new Point(sprite.Position.X + sprite.RenderSize.Width, sprite.Position.Y + RenderSize.Height);
return new Rect(sprite.Position, ptBottomRight);
}
}