开发者

how do you instanciate a class in c#?

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 o开发者_如何转开发n 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


Not to be pedantic to andrey but technically it's

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:

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;  
} 


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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜