Sign in to follow this  

Object Creation (OOP Related)

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on an asteroids clone and I've found an issue with the way my code is structured. Here's the setup:

I have a SpaceLevel class that contains 2 lists, a list of Projectiles and a list of other drawables. The list of other drawables contains the player which is simply updated and rendered like all other drawables. However, in my update method I want to request a new projectile be created when the user fires. At this point I have no reference back to the level from the ship class so I'm not sure what I should do...

A developer friend of mine mentioned I need some kind of 'create object' class that should be used throughout my code. Does anyone know what he's referring to or any references I can look at to solve this problem?

Share this post


Link to post
Share on other sites
If you want the player class to be decoupled from the SpaceLevel class, you can give it a FireParticle event, which will invoke a method to create a particle. That method will probably be in the SpaceLevel class, since it holds the particle list.

So the code will be something like:

player.FireParticle += SpaceLevel.CreateParticle;

Share this post


Link to post
Share on other sites
Your developer friend appears to be referring to the Factory pattern (try Google), although I can't see how it's relevant here.

Have you considered just passing a reference to the SpaceLevel as a parameter to the update function? How is the update function being called? What functionality does SpaceLevel provide - is it just a container for the drawable things, or is it actually do something beyond that?

Share this post


Link to post
Share on other sites
I would use events, events are OOP gods.

You could also use static properties. It might sound like a bad idea but if you're using a state management class you could just reference the current state and there you go. Really clean way to do it.

Share this post


Link to post
Share on other sites
I talked with him and he elaborated what I should do further. He had a lot of differing ways to do this but essentially boiled down to a singleton.

My problem I had a Level class that held a list of sprites, then I would draw all sprites for the current level. Well, none of this made any sense considering I have a restriction that you can only have 1 level loaded at a time. It makes more sense to have just a giant list of entities that get rendered and I can load/reload that list when I need to.

What I finally ended up with was a Singleton that holds all my sprites and all of the sprites in it just get rendered by the level. I had completely blanked and forgot that getting the instance in a singleton is a static call which is why I didn't understand how to call it.

@Zahlman, I wouldn't be able to pass it in as a seperate parameter because the update call is overridden and I would have to change both the subclass as well as the base class and all inheriting classes.

Share this post


Link to post
Share on other sites

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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