我怎么能减小碰撞时的生活,用XNA用C#? [英] How can I decrement lives on collision, using XNA with C#?

查看:117
本文介绍了我怎么能减小碰撞时的生活,用XNA用C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分配为我的电脑绘图类,是我们必须改变游戏代码。类本身是完全讲理论,所以我们几乎必须弄清楚自己的代码,我从来没有在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.

每当我的球员精灵是由一箱打,我想要的生活变量设置为1。然而递减,每当它被击中它只是不断下降。我明白为什么发生这种情况,但我不知道如何阻止它。

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. 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.

你能做些什么来解决这个是当物体发生碰撞,设置一个布尔值,指出该物体相撞启动然而,许多秒的计时器。当布尔值设置为true,当物体发生碰撞也不会递减的生活,但如果是假的就会递减。一旦启动计时器,将值设置为false,这样可以保持发生碰撞。这给人的对象时离开其他对象的内脏

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. Once the timer is up, set the value to false so they can keep colliding. This gives the object time to leave the 'innards' of the other object.

编辑:

这是一个非常简单的修复,可能不会在你的情况下工作(因为我不知道游戏的规则)。一个更完整的解决方案将是推动对象仅仅相隔不够,他们不再碰撞或者预测未来一个更新刻度,每个对象将会和防止他们曾经实际发生碰撞。

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.

这篇关于我怎么能减小碰撞时的生活,用XNA用C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆