如何实例化 C# 中的类?
我正在使用 XNA 框架 C# 为 Windows Phone 制作游戏。
游戏中的主要玩家必须射击。我有一个项目符号类,但是每次用户单击屏幕时如何实例化该项目符号?
子弹类基本上会绘制自身,并有一个名为“射击”的函数,该函数用于让子弹朝玩家的方向移动。
我是 c# 菜鸟 xD
I am making a game for the Windows Phone using XNA framework C#.
The main player in the game has to shoot. I have a bullet class, but how do you instantiate that bullet everytime the user clicks on the screen?
The bullet class basically draws itself and has a function called "Shoot", this is used for the bullet to move in the direction of the player.
I am a noob at c# xD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不是对安德烈迂腐,但从技术上讲是这样的
Not to be pedantic to andrey but technically it's
一种常见的方法是在游戏循环中添加对屏幕被触摸的检查,如果是则采取行动。
对于 XNA,在 Game 类(我认为默认情况下称为 Game1)上创建一个字段来存储在上一个循环中是否触摸了屏幕:
这是为了防止在单个循环上创建多个子弹触摸(除非那是您想要的)。
然后在 Game 类的
Update
方法中检查屏幕当前是否被触摸并采取行动:A common method is in your game loop add in a check for the screen being touched and take action if it is.
For XNA, on the Game class (which I think is called
Game1
by default) create a field to store whether the screen was touched on the previous Loop:This is to prevent multiple bullets being created on a single touch (unless that is what you want).
Then in the
Update
method of the Game class check to see if the screen is currently being touched and take action:如果您在实例化类时遇到困难,我想您也不熟悉内容管道,因此假设有一个基本的项目符号结构,我会执行以下操作:
要加载Texture2D,请使用如下代码:
我认为你的 Bullet 类有一个构造函数,它接受 Texture2D 和其他参数,使用这样的代码来实例化它:
另外,查看 http://GameDev.StackExchange.com ,特别是这个问题。
If you're having trouble instantiating a class, I presume you're also not familiar with the Content Pipeline either, so assuming a basic bullet structure, I'd do something like this:
To load a Texture2D use code like this:
I presume your Bullet class has a constructor which takes a Texture2D and other parameters, use code like this to instantiate it:
Also, checkout http://GameDev.StackExchange.com, and specifically this question.
由于这是一个游戏,我建议您不要在更新循环期间实例化对象。因此,您在玩游戏时会遇到非常严重的卡顿和滞后。
您想要执行此操作的方法是在游戏初始化时或在内容加载阶段,创建一个包含 100 个子弹的队列:
然后用 100 个子弹实例填充该列表:
在您的游戏中,当您需要发射一颗子弹,只需将其中一颗出列,设置它的速度、位置等并让它渲染。当它不再可见时(即击中某物、超出屏幕范围等),请再次将其入队。
这样,您就不会在游戏循环中实例化任何内容,而只是回收相同的对象。如果您在游戏中尽可能多地使用此操作,您将获得非常流畅的游戏体验。如果您养成了在更新循环中创建对象的习惯,Compact Framework 的垃圾收集器(手机上的垃圾收集器)将在某个时刻启动以清理它们,并会降低游戏的帧速率。
希望有帮助:)
Since this is a game, I would recommend that you do NOT instantiate objects during the Update loop. You will experience very bad stutter and lag during game play because of it.
The way you want to do it is at the initialization of the game, or in the content loading stage, is to create a Queue of say 100 bullets:
Then fill that list with 100 instances of bullet:
In your game, when you need to shoot a bullet, simply Dequeue one of those, set it's speed, position, etc and let it render. When it's no longer visible (i.e. hit something, went out of screen bounds, etc), Enqueue it again.
This way you are not instantiating anything in the game loop and just recycling the same objects. If you do this with as many things in your game as you can, you will have very smooth gameplay. If you get in the habit of creating objects in the update loop, the Garbage Collector for the Compact Framework (the one on the Phone) will kick in at some point to clean them up and will demolish your game's frame rate.
Hope that helps :)