在 XNA 游戏中记分

发布于 2024-08-31 12:57:36 字数 659 浏览 1 评论 0原文

我正在遵循 XNA 教程,并具有以下用于碰撞检测的代码(检测子弹何时与目标碰撞)。基本上,我希望增加一个分数变量以将分数显示到屏幕上,而无需重写整个程序。无论我将其放置在该方法中的哪个位置,它似乎都是从目标数量开始递增,而不是从零开始。我在这里缺少一些简单的东西吗?任何帮助将不胜感激。谢谢。

private CollisionType CheckCollision(BoundingSphere sphere)
{
    if (completeCityBox.Contains(sphere) != ContainmentType.Contains)
        return CollisionType.Boundary;

    for (int i = 0; i < targetList.Count; i++)
    {
        if (targetList[i].Contains(sphere) != ContainmentType.Disjoint)
        {
            targetList.RemoveAt(i);
            i--;
            AddTargets();
            return CollisionType.Target;
        }
    }

    return CollisionType.None;
}

I'm following an XNA tutorial and have the following code for collision detecting (detecting when a bullet collides with a target). Basically I'm looking to increment a score variable to display the score to the screen without re-writing the whole program. No matter where I place it in this method it seems to start incrementing from the number of targets, not from zero. Is there something simple I'm missing here? Any help would be greatly appreciated. Thanks.

private CollisionType CheckCollision(BoundingSphere sphere)
{
    if (completeCityBox.Contains(sphere) != ContainmentType.Contains)
        return CollisionType.Boundary;

    for (int i = 0; i < targetList.Count; i++)
    {
        if (targetList[i].Contains(sphere) != ContainmentType.Disjoint)
        {
            targetList.RemoveAt(i);
            i--;
            AddTargets();
            return CollisionType.Target;
        }
    }

    return CollisionType.None;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2024-09-07 12:57:36

您可以简单地使用一个名为 OnCollision() 的单独方法来更新分数(如果您稍后需要,可以为您执行任何物理操作),并在 Update 方法中使用 if 语句检查碰撞。比如:

if( CheckCollision() == CollisionType.Target )
{
   OnCollision();
}

希望有帮助。

射线

You could simply have a separate method called OnCollision() that updates the score (and could perform any physics for you if you wanted later on) and in your Update method, just have an if statement checking for collisions. Something like:

if( CheckCollision() == CollisionType.Target )
{
   OnCollision();
}

Hope that helps.

Ray

戏蝶舞 2024-09-07 12:57:36

假设您没有在其他任何地方递增/分配分数变量,并且在初始化期间将其设置为零,我能想到的唯一的另一件事是您以某种方式触发了与所有对象的碰撞 - 这是否可能以某种方式,也许在设置阶段?您是否尝试过在分数增量线上设置断点以查看它何时被击中?

Assuming you're not incrementing / assigning the score variable anywhere else, and it's set to zero during initialization, the only other thing I can think of is that somehow you're triggering a collision with all of your objects - is that possible somehow, perhaps during the setup phase? Have you tried setting a breakpoint on your score increment line to see when it gets hit?

七月上 2024-09-07 12:57:36

如果您在类的顶部初始化它,并且仅在第二个 if 语句中递增它,那么听起来好像所有项目都必须在第一次发生碰撞。

如果没有其余的代码,可能很难确定,但例如在 AddTargets 函数中,如果它们从相同的位置开始,然后进行调整,则可能在检查此碰撞时它们都合格,或者具有这种性质的东西。

如上所述,要么在更新分数时设置断点 - 如果需要,可以通过属性进行更新,并在属性上设置断点,以便您可以查看其被调用的位置并追溯其被调用的原因。如果在您实际发生预期的碰撞之前它从 0 到 250,那么应该很容易找到计数器更新不正确的位置,因为听起来它发生在您预期的第一次碰撞之前,因此添加前 250 点。

if you are initialing it at the top of your class, and only incrementing it in the second if statement it sounds like the collision has to be happening with all items the first time.

without the rest of the code it might be hard to determine but for example in the AddTargets function if they start with the same location, and then are adjusted it could be possible that when this collision is checked they all qualified, or something of that nature.

As stated above, either set a break point when you update the score - if needed update it via a property and set a break point on the property so you can see where its being called and track back why it is being called. If its going from 0 to 250 before you actually have what you expect to be a collision, it should be easy to track down where the counter is being updated incorrectly, as it sounds its happening before what you expect to be your first collision and thus adding the first 250 points.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文