我想让它当我点击敌人时,一个弹药出现在屏幕上

  • 本文关键字:一个 屏幕 敌人 monogame
  • 更新时间 :
  • 英文 :


这是我的代码片段(我已经有了它,所以当你点击一个幽灵弹药产卵,我只是没有包括,因为它是在一个过程中。)我的问题是弹药的RNG刷出;它一开始工作得很好,然后像20秒后,它只是不产卵。

private void ammoSpawn()
{
ammoSpawner = rng.Next(1, 101);
if (ammoSpawner > 50)
{
ammoTimer.Activate();
if (ammoTimer.IsActive())
{
ammoRec.X = rng.Next(100, 1720);
ammoRec.Y = rng.Next(100, 700);
if (ammoTimer.IsFinished())
{
ammoRec.X = -100;
ammoRec.Y = -100;
ammoTimer.ResetTimer(true);
}
}
}
}

感谢您的帮助

我猜你是在重复使用相同的弹药对象,并且变量如ammoSpawnerammoTimer是game1中的公共变量。

我建议为弹药对象创建一个单独的类。因此,所有相关代码都可以保留在弹药对象中,这也使创建多个项目变得更加容易。(以防将来你要展开) 在Ammo类中创建方法的例子:
int spawner = 0;
int timer = 0;
Texture2D ammoSprite = new Texture2D;
Rectangle rec = new Rectangle(x, y, width, height);
public void Ammo(Texture2D ammoSprite, _spawner, _timer) //this should be the constructor that creates the class
{
spawner = _spawner;
timer = _timer;
rec.X = rng.Next(100, 1720);
rec.Y = rng.Next(100, 700);
}
public void Update()
{
//function where you put your update logic for the ammo object
}
public void Draw(Spritebatch spriteBatch)
{
//function where you put your draw logic for the ammo object
spriteBatch.Draw(ammoSprite, rect, Color.White);
}

然后在你将使用弹药类的地方:

public Ammo ammo = null; //declared as a field on top of the page
private void ammoSpawn()
{
Ammo ammo = new Ammo(); //creating an Ammo object, should be inside
}
public void Update()
{
ammo.Update();
}
public void Draw()
{
SpriteBatch spriteBatch = new SpriteBatch();
ammo.Draw(spriteBatch);
}

为对象创建类是c#中的一个核心概念,以便能够重用它们。这也允许你创造多个行为不同的道具/敌人,但仍然保持基本功能。我强烈建议你去学习课程。

最新更新