为什么我的射弹没有绘制在屏幕上?
因此,我正在开发一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你能移动一下吗?
一旦你按下按钮,你就永远不会再进入了。
编辑 1
此外,您的射弹中的这一点
必须如下所示:
由于您仅使用这些来获取子弹的原点和目的地,因此如果您只在外部计算这些内容并通过初始化传递它们会更好。 (因为它会消除 Projectile 需要知道 Game1 和 Player 对象是什么样子)
您也可以完全删除 Initialize 方法并仅使用 Projectile 的构造函数,这看起来更自然。 (如果您为此使用构造函数,那么您知道任何时候您有一个实例,您都可以使用它而无需调用其他方法)
稍微编辑
我认为您真的不需要 Projectile 类中的bulletOrigin,您可以只需使用该位置,并将其从那里移动到项目符号目的地。
另外,Vector2类是用运算符重载方法制成的,因此您可以使用
而不是
其他,您的代码看起来不错!告诉我这些更改后是否仍然不起作用。另外,使用调试器单步调试它,看看是否可以找到任何有趣的东西。 :)
For start, could you move
Once you press your button, you're never gonna enter that if again.
Edit 1
Furthermore, this in your Projectile
has to look like:
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
instead of
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. :)