简体   繁体   English

您如何在c#中实例化一个类?

[英]how do you instanciate a class in c#?

I am making a game for the Windows Phone using XNA framework C#. 我正在使用XNA框架C#为Windows Phone制作游戏。

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 我是C#xD的菜鸟

并不是对Andrey的痴迷,但从技术上讲,它是

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

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: 对于XNA,在Game类(我认为默认情况下称为Game1 )上创建一个字段来存储是否在先前的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: 然后在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;  
} 

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: 如果您在实例化类时遇到麻烦,我想您也不熟悉Content Pipeline,因此假设基本的项目符号结构,我将执行以下操作:

To load a Texture2D use code like this: 要加载Texture2D,请使用如下代码:

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: 我假设您的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);

Also, checkout http://GameDev.StackExchange.com , and specifically this question . 另外,请检出http://GameDev.StackExchange.com ,尤其是这个问题

Since this is a game, I would recommend that you do NOT instantiate objects during the Update loop. 由于这是一个游戏,因此建议您不要在Update循环期间实例化对象。 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: 您要做的方法是在游戏初始化时或在内容加载阶段中,创建一个包含100个子弹的队列:

Queue<Bullet> bulletCache;

Then fill that list with 100 instances of bullet: 然后在列表中填充100个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 (ie 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 :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM