Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多向子弹[阅读]_C#_Vector_Xna_Bullet - Fatal编程技术网

C# 多向子弹[阅读]

C# 多向子弹[阅读],c#,vector,xna,bullet,C#,Vector,Xna,Bullet,一段时间以来,我一直在为一个我还在为学校工作的项目编写代码,尽管已经完成了 我想制造一枚手榴弹,可以向与之相撞的敌人发射子弹,但我需要atm的帮助是同时向所有不同方向发射8发子弹 这是老师给我的代码(我向他寻求帮助) if(mouse.LeftButton==ButtonState.Pressed) { 纹理为2D的锡织品; pewTexture=game.Content.Load(“pew”); game.firingSound.Play(); //调整长椅以向鼠标射击,而不仅仅是沿y轴笔直移

一段时间以来,我一直在为一个我还在为学校工作的项目编写代码,尽管已经完成了

我想制造一枚手榴弹,可以向与之相撞的敌人发射子弹,但我需要atm的帮助是同时向所有不同方向发射8发子弹

这是老师给我的代码(我向他寻求帮助)

if(mouse.LeftButton==ButtonState.Pressed)
{
纹理为2D的锡织品;
pewTexture=game.Content.Load(“pew”);
game.firingSound.Play();
//调整长椅以向鼠标射击,而不仅仅是沿y轴笔直移动。
pew=新的CPew(游戏、锡织品);
pew.velocity=新矢量2((浮点)数学Cos(旋转),(浮点)数学Sin(旋转))*10f+spriteVelocity;
皮尤位置=位置+皮尤速度*5;
//简单的奶酪方式:
//位置=位置;
//皮尤位置X+=15f;
//皮尤位置Y-=20f;
//超级酷的酷的方式,为酷的人和名人
位置+
新矢量2(
纹理.Width*.2f-pew.texture.Width*.2f,
-皮尤、质地、高度);
game.pews.Add(pew);
myFiringDelay=10;
}
其他的
{
if(mouse.RightButton==ButtonState.Pressed)
{
float pi2=(float)Math.PI*2.0f;
int numShots=10;
浮点数pi2overnum=pi2/numShots;
对于(int i=0;i
显然,这将使它在鼠标右键点击时向各个方向射击,但每次我尝试它,我的游戏就会直接崩溃 然后我们有一个皮尤课,是子弹课,这就是我想同时在这些方向上被射中的东西

你们可能无法帮助我展示给你们的代码,我花了一段时间寻找一种方法来实现这一点,但我似乎找不到任何东西

以前的示例和/或源代码将非常有用,至少可以用另一种方式来看待这一点,谢谢


当它崩溃时显示给我看,它告诉我索引超出范围或为负值,如果你们能给我看多向子弹的基本代码,我会很高兴的

问题是,当你们尝试在游戏中循环时,你们试图调用Reset()在集合的前10个项目上。但是其中似乎没有10个长椅。因此,当您到达末尾并尝试访问下一个长椅时,会出现“索引超出范围”错误

对于你的8颗手榴弹,我想你应该这样做

//Once a grenade has collided with an enemy 
float pi2 = (float)Math.PI * 2.0f;
int numShots = 8;
float pi2overnum = pi2 / numShots;

for (int i = 0; i < numShots; i++)
{
   Vector2 direction = PiToVec2(pi2overnum * i);

   pew = new CPew(game, pewTexture);

   pew.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 10f + spriteVelocity;

   //Set the position based off of the grenades last position.
   //Set the direction.
   //Set any other attributes needed.

   game.pews.Add(pew);
}

如果您的游戏正在崩溃,那么从调试器获取相关的输出/错误将非常有帮助,以便我们能够帮助您解决这一问题。在整个方法上放置一个try catch,并查看抛出的错误。此外,根据错误,我们可能需要知道什么是pews[]是的。而且每次点击都加载纹理是个坏主意,你可能想将其设置为参考。它超出了范围的哪一行?这意味着如果只有6个“长椅”,你就不能做长椅[7].所以从我看到的,当你右键单击它在10张长椅上循环时,有什么不是10?看看stacktrace,告诉我们什么超出了范围。索引超出了范围。必须是非负的,并且小于集合的大小。参数名称:indexI更改了我在玩家类中的内容,我将foreach循环放在我的game.cs中,因为它ldnt在player.cs中工作,我不断得到这个信息,集合被修改;枚举操作可能无法执行。我在左键单击时定期拍摄长椅有关系吗?如果有人可以,一个完整的手榴弹示例将非常有用,谢谢!
//Once a grenade has collided with an enemy 
float pi2 = (float)Math.PI * 2.0f;
int numShots = 8;
float pi2overnum = pi2 / numShots;

for (int i = 0; i < numShots; i++)
{
   Vector2 direction = PiToVec2(pi2overnum * i);

   pew = new CPew(game, pewTexture);

   pew.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 10f + spriteVelocity;

   //Set the position based off of the grenades last position.
   //Set the direction.
   //Set any other attributes needed.

   game.pews.Add(pew);
}
foreach CPew pew in game.pews
{
   pew.Update();
}