Jump to content
  • Advertisement
Sign in to follow this  
jrmiller84

Simple class question

This topic is 4814 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

Hey everyone, I have a pretty simple question whose answer I cant seem to find anywhere. Is there a way to determine if an instance of a class exists? Basically I have an array of classes that amount to blocks, when the ball collides with the blocks, they need to disappear. Now, I know the easy way out would be to just set a bool value that states that it has not been hit and once it's hit, set it to true and then don't render that block, but I'm looking for something a little more elegant so I don't waste resources. I've tried a few different things with no luck. Any help would be great. Thanks everyone.

Share this post


Link to post
Share on other sites
Advertisement
I would use a linked list to hold the blocks. When the ball hits one simply remove it. Check out stl::list.

edit: Untested example
#include <list>
#include "Block.hpp"
#include "Ball.hpp"

int main() {

stl::list<Block> blocks;
Ball myBall(30,50);

// add blocks to the list here
blocks.push_back(Block(25,25));
blocks.push_back(Block(50,25));

// main loop
while (!gameOver) {

// check every block to see if it touches the ball
for(stl::list<Block>::iterator b = blocks.begin(); b != blocks.end(); b++) {
if (myBall.collides(b)) {
b = blocks.erase(b);
}
}

}

}

Share this post


Link to post
Share on other sites
You could use a linked list, or if you want to access it like an array you could create a vector (or array, whatever) of pointers, then allocate them individually, when the ball hits that block you deallocate it (the block, not the ball) and set to null, thus you are freeing up memory and you are able to determine if the instance exists or not.

Edit: Of course if you don't want to use dynamic memory, you could use bitwise freakiness or bitvectors, you'd still have boolean values, but they'd only be 1bit, you'd be suprised how much information you can cram into 32bits.

Share this post


Link to post
Share on other sites
for determining if an instance if class exist, you can use Singletone class.
Basicly this class looks like:

class Singleton
{
public:
static Singleton * instance ();

protected:
Singleton ();

private:
static Singleton * m_pInstance;
};

Singleton * Singleton :: m_pInstance = 0L;

Singleton * Singleton :: instance ()
{
return ((m_pInstance ? m_pInstance
: (m_pInstance = new Singleton)));
}

now you can check with instance call if there is instance of something, and if not it will be created...
To get more sophisticated source code just do some google search..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!