为什么我的射弹没有绘制在屏幕上?

发布于 2024-12-27 02:28:03 字数 351 浏览 2 评论 0原文

因此,我正在开发一个简单的 C# 程序,它允许您在图形中移动,并让它在单击鼠标时发射从玩家到鼠标位置的子弹。我对 C# 还很陌生,但对 Java 和 Python 有一些经验。

我完全能够使用 WASD 移动我的角色,但无法绘制子弹,更不用说让它们在更新时移动了。

bug 可能出在哪里?

弹丸对象的绘制方法? Game1 类的用户输入更新方法? 弹丸物体的方向设置?

这是完整的代码: http://pastebin.com/j5QVLKU3

我遗漏了玩家类,但它只有几个玩家健康等变量。

So I am developing a simple C# program that allows you to move around a graphic and have it fire bullets that travel from the player to the position of the mouse at the time of the mouse click. I'm still new to C# but have some experience with Java and Python.

I am fully able to move my character with the WASD but unable to get the bullets to be drawn let alone get them to move as they update.

Where might the bug be?

The projectile object's draw method?
The Game1 class's update method for the user input?
The projectile's object's direction setting?

Here is the full code:
http://pastebin.com/j5QVLKU3

I left the player class out, but it has nothing more then a few player variables like health.

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

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

发布评论

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

评论(1

撩心不撩汉 2025-01-03 02:28:03

首先,你能移动一下吗?

mouseStateCurrent = Mouse.GetState();

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
    if (mouseStatePrevious.LeftButton != ButtonState.Pressed)
    {
        AddProjectile(player.positionPlayer);

        mouseStatePrevious = mouseStateCurrent; //<-- This line
    }
}
//<-- Here

一旦你按下按钮,你就永远不会再进入了。

编辑 1

此外,您的射弹中的这一点

    Player playerObject = new Player();
    Game1 gameObject = new Game1();

必须如下所示:

    Player playerObject;
    Game1 gameObject;

    public Projectile(Player player, Game1 game)
    {
        playerObject = player;
        gameObject = game;
    }

由于您仅使用这些来获取子弹的原点和目的地,因此如果您只在外部计算这些内容并通过初始化传递它们会更好。 (因为它会消除 Projectile 需要知道 Game1 和 Player 对象是什么样子)

您也可以完全删除 Initialize 方法并仅使用 Projectile 的构造函数,这看起来更自然。 (如果您为此使用构造函数,那么您知道任何时候您有一个实例,您都可以使用它而无需调用其他方法)

稍微编辑

我认为您真的不需要 Projectile 类中的bulletOrigin,您可以只需使用该位置,并将其从那里移动到项目符号目的地。

另外,Vector2类是用运算符重载方法制成的,因此您可以使用

positionBullet += directionLine*velocity;

而不是

positionBullet = Vector2.Add(positionBullet, directionLine * velocity);

其他,您的代码看起来不错!告诉我这些更改后是否仍然不起作用。另外,使用调试器单步调试它,看看是否可以找到任何有趣的东西。 :)

For start, could you move

mouseStateCurrent = Mouse.GetState();

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
    if (mouseStatePrevious.LeftButton != ButtonState.Pressed)
    {
        AddProjectile(player.positionPlayer);

        mouseStatePrevious = mouseStateCurrent; //<-- This line
    }
}
//<-- Here

Once you press your button, you're never gonna enter that if again.

Edit 1

Furthermore, this in your Projectile

    Player playerObject = new Player();
    Game1 gameObject = new Game1();

has to look like:

    Player playerObject;
    Game1 gameObject;

    public Projectile(Player player, Game1 game)
    {
        playerObject = player;
        gameObject = game;
    }

Since you're using those only to get the origin and destination for the bullet, it would be better if you just calculated those outside, and passed them with the initialize. (because it would remove Projectile's need to know what Game1 and Player objects looked like)

You could also completely remove the Initialize method and just use Projectile's constructor, that seems more natural. (if you use the constructor for this, then you know that any time you have an instance, you can use it without having to call additional methods)

Slight edit

I don't think you really need the bulletOrigin in your Projectile class, you could just use the position, and move it from there to the bulletDestination.

Also, Vector2 class is made with operator overloaded methods, so you could use

positionBullet += directionLine*velocity;

instead of

positionBullet = Vector2.Add(positionBullet, directionLine * velocity);

Other than those, your code seems fine! Tell me if it still doesn't work after these changes. Also, use your debugger to step into it, see if you can find anything interesting. :)

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