简体   繁体   中英

How does one refer to the game1 class's variables in XNA

I try to refer to an array in the Game1 class by creating an object of the Game1 class with its definition Game1 gameObject; , its declaration gameObject = new Game1(); in the constructor, and its usage gameObject.tileArray[xInt, yInt].treed = true; .

The game1 class itself remains unchanged in its class code other than some methods and variables. When I try to use this code in the TreeRegion class it gives me "Object reference not set to an instance of an object," leading me to believe that the game1 class is null somehow.

How might one change the class or object in order to refer to game1's variables? Mind you I am self-taught and still rather new to C#.

The class that refers to game1: http://pastebin.com/0chEcKfq Game1 itself http://pastebin.com/zLDVzCca

When I try to use this code in the TreeRegion class it gives me "Object reference not set to an instance of an object," leading me to believe that the game1 class is null somehow.

Not necessary. Look at your expression:

gameObject.tileArray[xInt, yInt].treed = true;

That will throw a NullReferenceException if any of these are true:

  • gameObject is null (unlikely, given the code you've shown)
  • gameObject.tileArray is null
  • gameObject.tileArray[xInt, yInt] is null

You haven't shown us the Game1 class - it seems very likely that either the second or third bullet is the problem here.

You should probably consider whether this should actually be part of the Game1 API rather than digging down the level like this:

game1.MakeTree(xInt, yInt);

... or whatever the better name might be. It certainly looks like you're missing some encapsulation here.

(Personally I wouldn't really recommend learning C# via something like XNA. I think while you're still at the stage of learning the language and core libraries, it's a better idea to work in a simple environment such as console applications. I realize they may seem boring, but a bit of groundwork up-front may make a huge different in the long run.)

EDIT: Now we can see Game1 (which could do with a new name - and you should try to follow the .NET naming conventions ) I strongly suspect that createTiles is causing the problem. It's not at all obvious that that will actually populate every element of tileArray - particularly as you never even use a or b . My guess is that gameObject.tileArray[xInt, yInt] is returning null in your TreeRegion code.

EDIT: Looking at how you're creating the array, it's even more concerning:

gridWidth = 100; gridHeight = 100;
gridScope = gridWidth * gridHeight;
tileArray = new Tile[gridScope, gridScope];

Did you really want a 10000x10000 array? Are you sure you need this gridScope ? I would have expected:

tileArray = new Tile[gridWidth, gridHeight];

Note that in createTiles you're only populating at most gridHeight * gridWidth elements (ie ten thousand out of the ten million elements in the array).

There is a wonderful debugger built into Visual Studio.

Though I cannot tell with out the Game1 code, I would assume that the array your are accessing is null.

gameObject.tileArray needs to be instanciated in the GameObject Constructor

so

Game1()
{
    tileArray = new Tile[]();

}

Assuming Tile is the class that is in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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