C# 移动列表中的投射物
我有一个可以发射射弹的对象,我试图使用一个列表来生成火箭(射弹),这样当它们与一个对象碰撞时我就可以删除它们。所以我首先创建C# 移动列表中的投射物,c#,list,animation,xna,game-physics,C#,List,Animation,Xna,Game Physics,我有一个可以发射射弹的对象,我试图使用一个列表来生成火箭(射弹),这样当它们与一个对象碰撞时我就可以删除它们。所以我首先创建列表火箭然后我添加了一个功能,让火箭不断地发射: if (Time > 0.2) { Time = 0; Rockets.Add(new Rectangle((int)Position.X, (int)Position.Y, rocketTexture.Width, rocketText
列表火箭代码>然后我添加了一个功能,让火箭不断地发射:
if (Time > 0.2)
{
Time = 0;
Rockets.Add(new Rectangle((int)Position.X, (int)Position.Y, rocketTexture.Width, rocketTexture.Height));
}
然后,我尝试更新它们,以便它们通过执行foreach在屏幕上移动:
foreach (Rectangle r in Rockets)
{
}
问题
这就是我被卡住的地方,我如何调用每个火箭列表中的x和y值,以便在屏幕上移动它
我可能想得太多了,有一种更简单的方法可以创建大量投射物,并在与道路相撞或它们走得太远时使其消失。在游戏开发中,您更愿意使用update()方法实现火箭类,在该方法中,您可以通过一些speed_x和speed_y属性移动火箭。然后,您可以在main run()方法中通过调用Rocket.getRect()检查冲突(或者只调用.Rect属性,该属性可以在父类实体或其他对象中实现)
话虽如此,我可能还不理解这个问题。在游戏开发中,您更愿意使用update()方法实现一个火箭类,在这个类中,您可以通过一些speed\u x和speed\u y属性移动火箭。然后,您可以在main run()方法中通过调用Rocket.getRect()检查冲突(或者只调用.Rect属性,该属性可以在父类实体或其他对象中实现)
话虽如此,我可能还不明白问题所在。为
循环执行,而不是为每个
循环执行。我还建议使用数组(并创建一个Rocket
类,而不仅仅是vector2)
for(int i=0;i
对
循环执行,而不是对每个
循环执行。我还建议使用数组(并创建一个Rocket
类,而不仅仅是vector2)
for(int i=0;i
下面是一个代码示例,希望对您有所帮助
class Rocket
{
int _life;
public Rocket(Vector2 position, Vector2 direction)
{
_position = position;
_direction = direction;
//life of rocket. Once it reaches zero the rocket is removed.
_life = 100;
}
Vector2 _position
public Vector2 Position
{
get
{
return _position;
}
}
Vector2 _velocity;
Vector2 Velocity
{
get
{
return _velocity;
}
}
public int Life
{
get
{
return _life;
}
}
public void Update(World world)
{
_life--;
_position += _velocity;
//could check for collisions here, eg:
foreach (Ship ship in world.Ships)
{
if (Intersects(ship))
{
//collision!
//destroy this rocket
_life = 0;
//damage the ship the rocket hit
ship.ApplyDamage(10);
return;
}
}
}
}
class Game
{
List<Rocket> _rockets = new List<Rocket>();
List<Rocket> _deadRockets = new List<Rocket>();
void Update(GameTime gameTime)
{
//.ToArray() is used here because the .net list does not allow items to be removed while iterating over it in a loop.
//But we want to remove rockets when they are dead. So .ToArray() means we are looping over the array not the
//list, so that means we are free to remove items from the list. basically it's a hack to make things simpler...
foreach (Rocket rocket in _rockets.ToArray())
{
//world is an object that contains references to everything on the map
//so that the rocket has access to that stuff as part of it's update
rocket.Update( world );
//if the rocket has run out of life then remove it from the list
if (rocket.Life <= 0)
_rockets.Remove(rocket);
}
}
void FireRocket(Vector2 from, Vector2 direction)
{
Rocket newRocket = new Rocket(from, direction);
_rockets.Add(newRocket);
}
}
class火箭
{
国际生命;
公共火箭(矢量2位置,矢量2方向)
{
_位置=位置;
_方向=方向;
//火箭的寿命。一旦它达到零,火箭就被移除。
_寿命=100;
}
矢量2_位置
公共向量2位置
{
得到
{
返回位置;
}
}
矢量2_速度;
矢量2速度
{
得到
{
返回速度;
}
}
公共生活
{
得到
{
回归生活;
}
}
公共空间更新(世界)
{
_生命--;
_位置+=\u速度;
//可以在此处检查碰撞,例如:
foreach(世界上的船。船)
{
如果(与(船舶)相交)
{
//碰撞!
//摧毁这枚火箭
_寿命=0;
//火箭击中的船只受损
船舶损坏(10);
返回;
}
}
}
}
班级游戏
{
列表_=新列表();
列表_=新列表();
无效更新(游戏时间游戏时间)
{
//此处使用.ToArray(),因为.net列表不允许在循环中对其进行迭代时删除项。
//但是我们希望在火箭死后移除它们。因此。ToArray()表示我们在阵列上循环,而不是在阵列上循环
//列表,这意味着我们可以自由地从列表中删除项目。基本上,这是一种使事情更简单的黑客行为。。。
foreach(火箭中的火箭。ToArray())
{
//世界是一个包含对地图上所有事物的引用的对象
//这样火箭就可以访问这些东西,作为更新的一部分
火箭更新(世界);
//如果火箭已经耗尽了生命,那么将其从列表中删除
如果(rocket.Life这里有一个代码示例,希望对您有所帮助
class Rocket
{
int _life;
public Rocket(Vector2 position, Vector2 direction)
{
_position = position;
_direction = direction;
//life of rocket. Once it reaches zero the rocket is removed.
_life = 100;
}
Vector2 _position
public Vector2 Position
{
get
{
return _position;
}
}
Vector2 _velocity;
Vector2 Velocity
{
get
{
return _velocity;
}
}
public int Life
{
get
{
return _life;
}
}
public void Update(World world)
{
_life--;
_position += _velocity;
//could check for collisions here, eg:
foreach (Ship ship in world.Ships)
{
if (Intersects(ship))
{
//collision!
//destroy this rocket
_life = 0;
//damage the ship the rocket hit
ship.ApplyDamage(10);
return;
}
}
}
}
class Game
{
List<Rocket> _rockets = new List<Rocket>();
List<Rocket> _deadRockets = new List<Rocket>();
void Update(GameTime gameTime)
{
//.ToArray() is used here because the .net list does not allow items to be removed while iterating over it in a loop.
//But we want to remove rockets when they are dead. So .ToArray() means we are looping over the array not the
//list, so that means we are free to remove items from the list. basically it's a hack to make things simpler...
foreach (Rocket rocket in _rockets.ToArray())
{
//world is an object that contains references to everything on the map
//so that the rocket has access to that stuff as part of it's update
rocket.Update( world );
//if the rocket has run out of life then remove it from the list
if (rocket.Life <= 0)
_rockets.Remove(rocket);
}
}
void FireRocket(Vector2 from, Vector2 direction)
{
Rocket newRocket = new Rocket(from, direction);
_rockets.Add(newRocket);
}
}
class火箭
{
国际生命;
公共火箭(矢量2位置,矢量2方向)
{
_位置=位置;
_方向=方向;
//火箭的寿命。一旦它达到零,火箭就被移除。
_寿命=100;
}
矢量2_位置
公共向量2位置
{
得到
{
返回位置;
}
}
矢量2_速度;
矢量2速度
{
得到
{
返回速度;
}
}
公共生活
{
得到
{
回归生活;
}
}
公共空间更新(世界)
{
_生命--;
_位置+=\u速度;
//可以在此处检查碰撞,例如:
foreach(世界上的船。船)
{
如果(与(船舶)相交)
{
//碰撞!
//摧毁这枚火箭
_寿命=0;
//火箭击中的船只受损
船舶损坏(10);
返回;
}
}
}
}
班级游戏
{
列表_=新列表();
列表_=新列表();
无效更新(游戏时间游戏时间)
{
//此处使用.ToArray(),因为.net列表不允许在循环中对其进行迭代时删除项。
//但是我们希望在火箭死后移除它们。因此。ToArray()表示我们在阵列上循环,而不是在阵列上循环
//列表,这意味着我们可以自由地从列表中删除项目。基本上,这是一种使事情更简单的黑客行为。。。
foreach(火箭中的火箭。ToArray())
{
//世界是一个包含对地图上所有事物的引用的对象
//这样火箭就可以访问这些东西,作为更新的一部分
火箭更新(世界);