简体   繁体   English

如何在C#中使用XNA来减少碰撞时的生命?

[英]How can I decrement lives on collision, using XNA with C#?

I have an assignment for my computer graphics class, were we have to alter a game code. 我有计算机图形课的作业,我们必须改变游戏代码。 The class itself is entirely about theory, so we pretty much have to figure out the code for ourselves and I've never programmed in C#, so sorry for any ignorance on my part. 这个类本身完全是关于理论的,所以我们几乎必须为自己找出代码并且我从未用C#编程,所以对于我的任何无知感到抱歉。

Whenever my player sprite is hit by a box, I want the lives variable to decrement by 1. However, whenever it gets hit it just keeps going down. 每当我的玩家精灵被一个盒子击中时,我希望生命变量减少1.然而,每当它被击中时它就会继续下降。 I understand why this is happening, but I don't know how to stop it. 我理解为什么会这样,但我不知道如何阻止它。

This is the code that checks wether the two objects have collided. 这是检查两个对象是否发生碰撞的代码。

if (b.BoundBox.Intersects(player.BoundSphere)){
    player.hit = true;
}

In the update function for the player I have: 在我有玩家的更新功能中:

if(hit){
    lives -= 1
}

How can I make it decrements only once, instead of constantly? 我怎样才能让它减少一次,而不是经常减少?

This is happening because it will constantly be detecting that it is colliding, as long as part of one of the objects is inside the other it will say that it is colliding. 这种情况正在发生,因为它会不断地检测到它正在发生碰撞,只要其中一个物体的一部分位于另一个物体内,它就会说它正在发生碰撞。

What you can do to work around this is when the objects collide, set a boolean value that says the objects have collided and start a timer for however many seconds. 你可以做些什么来解决这个问题,当对象发生碰撞时,设置一个布尔值,表示对象已经碰撞,并启动一个计时器,但是很多秒。 While the boolean value is set to true, when the objects collide it will not decrement the lives but if it is false it will decrement. 当布尔值设置为true时,当对象发生碰撞时,它不会减少生命,但如果它是假的,它将减少。 Once the timer is up, set the value to false so they can keep colliding. 计时器启动后,将值设置为false,以便它们可以保持冲突。 This gives the object time to leave the 'innards' of the other object. 这使得对象有时间离开另一个对象的“内部”。

Edit: 编辑:

This is a very simple fix and might not work in your situation (as i don't know the rules of the game). 这是一个非常简单的修复,可能不适用于您的情况(因为我不知道游戏的规则)。 A more complete solution would be to push the objects apart just enough that they are no longer colliding or predict one update tick ahead where each object will be and prevent them from ever actually colliding. 一个更完整的解决方案是将对象推得足够远,使它们不再发生碰撞或预测每个对象前面的一个更新滴答,并防止它们实际发生碰撞。

You don't need a timer for this problem, just declare another bool that keeps track if you've updated lives. 你不需要一个计时器来解决这个问题,如果你已经更新了生命,只需要声明另一个跟踪的bool

if (b.BoundBox.Intersects(player.BoundSphere) && !player.hit)
{
    player.hit = true;
    player.updated = false;
}

In your player's update: 在您的播放器更新中:

if (hit && !updated)
{
    lives -= 1
    updated = true;
}

However, if you want to use a counter simply declare a: 但是,如果您想使用计数器,只需声明:

TimeSpan timer = new TimeSpan(0, 0, x); //where x is how many seconds you need

and then in the Update method: 然后在Update方法中:

timer -= gameTime.ElapsedGameTime;
if (timer <= TimeSpan.Zero)
{
   // do what you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM