如何实例化 C# 中的类?

发布于 2024-10-20 20:48:21 字数 170 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

歌枕肩 2024-10-27 20:48:21

不是对安德烈迂腐,但从技术上讲是这样的

Bullet mybullet = new Bullet(A, B, C);

Not to be pedantic to andrey but technically it's

Bullet mybullet = new Bullet(A, B, C);
风柔一江水 2024-10-27 20:48:21

一种常见的方法是在游戏循环中添加对屏幕被触摸的检查,如果是则采取行动。

对于 XNA,在 Game 类(我认为默认情况下称为 Game1)上创建一个字段来存储在上一个循环中是否触摸了屏幕:

bool screenBeingTouched = false;

这是为了防止在单个循环上创建多个子弹触摸(除非那是您想要的)。

然后在 Game 类的 Update 方法中检查屏幕当前是否被触摸并采取行动:

TouchCollection newScreenTouches = TouchPanel.GetState(); 

if (!screenBeingTouched && newScreenTouches.Count > 0)  
{  
    screenBeingTouched = true;  

    Bullet myBullet = new Bullet();
    myBullet.DoSomething(); // Such as render on the screen and move around.

}  
else if (newScreenTouches.Count == 0)  
{  
    screenBeingTouched = false;  
} 

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:

bool screenBeingTouched = false;

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:

TouchCollection newScreenTouches = TouchPanel.GetState(); 

if (!screenBeingTouched && newScreenTouches.Count > 0)  
{  
    screenBeingTouched = true;  

    Bullet myBullet = new Bullet();
    myBullet.DoSomething(); // Such as render on the screen and move around.

}  
else if (newScreenTouches.Count == 0)  
{  
    screenBeingTouched = false;  
} 
暗藏城府 2024-10-27 20:48:21

如果您在实例化类时遇到困难,我想您也不熟悉内容管道,因此假设有一个基本的项目符号结构,我会执行以下操作:

要加载Texture2D,请使用如下代码:

var tx = this.Content.LoadContent<Texture2D>("TextureYouAddedAsContent");

我认为你的 Bullet 类有一个构造函数,它接受 Texture2D 和其他参数,使用这样的代码来实例化它:

int speed = 500;
Vector2 pos = new Vector2(50, 50); // start at 50, 50, top left
Vector2 dir = new Vector2(1, 0); // direction is in positive X direction
Bullet bullet = new Bullet(tx, pos, dir, speed);

另外,查看 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:

var tx = this.Content.LoadContent<Texture2D>("TextureYouAddedAsContent");

I presume your Bullet class has a constructor which takes a Texture2D and other parameters, use code like this to instantiate it:

int speed = 500;
Vector2 pos = new Vector2(50, 50); // start at 50, 50, top left
Vector2 dir = new Vector2(1, 0); // direction is in positive X direction
Bullet bullet = new Bullet(tx, pos, dir, speed);

Also, checkout http://GameDev.StackExchange.com, and specifically this question.

混浊又暗下来 2024-10-27 20:48:21

由于这是一个游戏,我建议您不要在更新循环期间实例化对象。因此,您在玩游戏时会遇到非常严重的卡顿和滞后。

您想要执行此操作的方法是在游戏初始化时或在内容加载阶段,创建一个包含 100 个子弹的队列:

Queue<Bullet> bulletCache;

然后用 100 个子弹实例填充该列表:

for (int i = 0; i < 100; i++)
    bulletCache.Enqueue(new Bullet());

在您的游戏中,当您需要发射一颗子弹,只需将其中一颗出列,设置它的速度、位置等并让它渲染。当它不再可见时(即击中某物、超出屏幕范围等),请再次将其入队。

这样,您就不会在游戏循环中实例化任何内容,而只是回收相同的对象。如果您在游戏中尽可能多地使用此操作,您将获得非常流畅的游戏体验。如果您养成了在更新循环中创建对象的习惯,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:

Queue<Bullet> bulletCache;

Then fill that list with 100 instances of bullet:

for (int i = 0; i < 100; i++)
    bulletCache.Enqueue(new 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 :)

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