Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to add my camera component to Game1 class' constructor like so:

Camera camera; // from  class Camera : GameComponent
....

public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        this.graphics.PreferredBackBufferWidth = screenWidth;
        this.graphics.PreferredBackBufferHeight = screenHieght;
        this.graphics.IsFullScreen = true;
        Content.RootDirectory = "Content";

        camera = new Camera(this);
        Components.Add(camera);
    }

From the just adding the last two lines, when I run the game, the screen freezes then gives me this message:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll

Additional information: The operation completed successfully

share|improve this question

closed as too localized by Patrick Hughes, Tetrad Feb 6 at 19:40

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

Figured it out...

this.Components.Add(camera); // "this." in the beginning 
share|improve this answer
5  
That makes no sense. "this" refs to the object itself. It doesn't look like you were out of that scope( which is impractical ). I think your problem lies elsewhere. – Sidar Sep 9 '12 at 18:59
@Sidar Its working for me. Maybe its the way I've create my camera which makes it different. – ChocoMan Sep 9 '12 at 22:06
1  
I'm thinking that you have more than 1 Game class, and thus you were adding it to some other Game Component lists. Specifying "this" would set it to the current object (probably your main Game). You shouldn't have more than 1 thing being derived from Game, or else you get strange errors like this. – electroflame Sep 10 '12 at 0:33
1  
If you remove the "this" keyword now. Would it crash? @electroflame that doesn't make much sense either. Even if you leave out the "this" keyword. It should still ref to itself, unless the "Components" property is not withing that scope. this keyword is usually used to point to itself or to distinguish between function variables and class members. – Sidar Sep 10 '12 at 9:31
1  
@electroflame Sidar Cypher I see why I had to use "this." The problem before was that I already had a variable named "camera" that I forgot to erase within the Game1 class. Now I can use it like normal. – ChocoMan Sep 10 '12 at 23:59
show 3 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.