我正在制作太空入侵者游戏,我在移动入侵者列表方面遇到了麻烦。问题是,当入侵者遇到viewport.width时,右边的入侵者与前一个入侵者重叠,应该反过来。
问题图片: http://i46.tinypic.com/20af4uh.png
希望下一个代码能解释这个问题:
public void Update(GameTime gameTime)
{
foreach (Invaders listInvader in listInvaders)
{
position = listInvader.GetPosition();
if (position.X > viewport.Width)
{
foreach (Invaders invaderse in listInvaders)
{
snelheid.X *= -1;
snelheid.Y = 100;
invaderse.Update(gameTime, snelheid);
}
}
else if (position.X <= 0)
{
foreach (Invaders invaderse in listInvaders)
{
snelheid.X *= -1;
snelheid.Y = 100;
invaderse.Update(gameTime, snelheid);
}
}
snelheid.Y = 0;
listInvader.Update(gameTime, snelheid);
}
入侵者更新:
public void Update(GameTime GameTime, Vector2 Snelheid)
{
//Update rectangle.
ObjectRectangle = new Rectangle((int)(Position.X), (int)(Position.Y), (int)(ObjectTexture.Width), (int)(ObjectTexture.Height));
//Update position
Position += Snelheid * (float)GameTime.ElapsedGameTime.TotalSeconds;
}