Skip to main content
gamedev.net
🔒 Locked

Simple class question

Started by jrmiller84 Sep 8, 2005 at 4:03 PM 4 replies 800+ views
Original Post
jrmiller84
jrmiller84
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.
I will forever be a student.
SiCrane
SiCrane
In what programming language?
jrmiller84
jrmiller84
I guess that would be helpful, sorry haha, C++
I will forever be a student.
MidoriKid
MidoriKid
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);           }        }    }}
Horatius83
Horatius83
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.
"Think you Disco Duck, think!" Professor Farnsworth
streamer
streamer
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..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.