Foreach 语句不包含 getEnumerator 的 public 定义
Foreach statement does not contain public definition for getEnumerator
我正在尝试实现两个桨之间的碰撞,但出现 GetEnumerator 错误。
这是我收到错误的地方:
foreach (Player item in paddle1)
{
item.Update(gameTime);
if (paddle2.CollisionSprite(item))
{
paddle2.Velocity = -paddle2.Velocity;
}
}
以及我的 Player 碰撞方法:
public virtual Circle CollisionCircle
{
get
{
return new Circle((int)(Position.X - SpriteOrigin.X * Scale), (int)(Position.Y - SpriteOrigin.Y * Scale),
(int)(TextureImage.Width * Scale), (int)(TextureImage.Height * Scale));
}
}
public bool CollisionSprite(Player sprite)
{
return CollisionCircle.Intersects(sprite.CollisionCircle);
}
错误是因为 paddle1 是 Player 类型,而不是 IEnumerable。如果你只有两个玩家,你不应该循环并且可以像这样实现你的功能:
public void CollisionDetection(Player firstPlayer, Player secondPlayer)
{
firstPlayer.Update(gameTime);
secondPlayer.Update(gameTime);
if (firstPlayer.CollisionSprite(secondPlayer))
{
firstPlayer.Velocity = -firstPlayer.Velocity;
secondPlayer.Velocity = -secondPlayer.Velocity;
}
}
用法为:
CollisionDetection(paddle1, paddle2);
我正在尝试实现两个桨之间的碰撞,但出现 GetEnumerator 错误。
这是我收到错误的地方:
foreach (Player item in paddle1)
{
item.Update(gameTime);
if (paddle2.CollisionSprite(item))
{
paddle2.Velocity = -paddle2.Velocity;
}
}
以及我的 Player 碰撞方法:
public virtual Circle CollisionCircle
{
get
{
return new Circle((int)(Position.X - SpriteOrigin.X * Scale), (int)(Position.Y - SpriteOrigin.Y * Scale),
(int)(TextureImage.Width * Scale), (int)(TextureImage.Height * Scale));
}
}
public bool CollisionSprite(Player sprite)
{
return CollisionCircle.Intersects(sprite.CollisionCircle);
}
错误是因为 paddle1 是 Player 类型,而不是 IEnumerable。如果你只有两个玩家,你不应该循环并且可以像这样实现你的功能:
public void CollisionDetection(Player firstPlayer, Player secondPlayer)
{
firstPlayer.Update(gameTime);
secondPlayer.Update(gameTime);
if (firstPlayer.CollisionSprite(secondPlayer))
{
firstPlayer.Velocity = -firstPlayer.Velocity;
secondPlayer.Velocity = -secondPlayer.Velocity;
}
}
用法为:
CollisionDetection(paddle1, paddle2);