Sign in to follow this  
-justin-

Release with Sprites in a class

Recommended Posts

Hey everyone, I'm working on yet another game. This'll be my fourth. However, this is my first time working with classes and DirectX, as well as linked lists (not my first time with the material itself, just in the DirectX sense). I'm getting an error saying this: an unhandled exception of type 'System.NullReferenceException' occurred. Additonal Information: Object reference not set to an instance of an object. Now basically, I have broken my class structure into a hierarchy or three things. Level, Scene,Entity (which represents a character or something). Each Scene and Entity have a Sprite and Texture associated with it. The debugger marks this as the error: [this is in Entity's deconstructor]
if(entityTexture != NULL)
{
  entityTexture->Release();
}

I also have a method inside Entity that "loads the graphic" which just looks like this:
void Entity::loadEGraphics(char *filename)
{
	if(FAILED(D3DXCreateSprite(g_pd3dDevice, &entitySprite)))
	{
		MessageBox(NULL, "Error", "Entity sprite load error!", MB_OK);
	}

	if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, filename, &entityTexture)))
	{
		MessageBox(NULL, "Error", "Entity Texture load error!", MB_OK);
	}
}

Why am I getting a NULL exception when I'm filling my EntityTexture?

Share this post


Link to post
Share on other sites
Don't mind me, I'm just an average idiot :P

Deconstructors get called everytime your object goes outta scope, so it was throwing an error.

Silly me...

Share this post


Link to post
Share on other sites
Hmm, well I thought that would fix it, however now it is saying the sprite object is a NULL value.

The only thing I can think of is that maybe my copy constructor and/or overload operator = isn't working. :/

here's the beginning of my Scene's operator overload; e is an entity with the SPRITE object and TEXTURE object

later scene calls a draw method that calls individual entity.draw methods

this is why i didn't work with classes b4 :P


Scene& Scene::operator = (Scene &other) throw(exception)
{
if(this != &other)
{
delete []e;
e = new Entity[other.numEntities];
if(e == NULL)
{
throw exception("Scene = error!");
}
else
{
for(int i = 0; i < other.numEntities; i++)
{
e[i] = other.e[i];
}
}




If any1 has any ideas I'd be really happy... or some example code of what you've done. This is probably really easy and I'm over-complicating it.

Also, not sure if I should use one sprite object / texture for each entity or not. Seems kinda wasteful!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this