| 12-28-2008, 10:37 PM | #1 |
o harro der Well I've been working on a small game lately, nothing special. It's my first real project in C++ so it's pretty cliche and seemingly simple. Here's basically what I've been trying to accomplish: You control a small spaceship. Your spaceship fires two kinds of weapons: linear beams and homing missiles fired respectively with the left and right mouse buttons. You battle an array of different enemies each with different characteristics. Here's how I went about defining the enemies. I have a parent class called 'ship': Code:
class ship
{
public:
// Some generic members go here like x,y position etc...
virtual void draw(SDL_Surface* screen) {}
virtual void loop(void) {}
virtual void explode(void) {}
};Now, there is one child class per enemy type: Code:
class enemy1: public ship
{
public:
void draw(SDL_Surface* screen)
{
// Draw the enemy here
}
void loop(void)
{
// Do things with the concerned object.
}
// etc..
};I stack all of the enemies up in a 'ship' array called 'ships', run through the array in the main loop and do: Code:
for(int i = 0;i<COUNT;i++)
{
ships[i]->loop();
ships[i]->draw(screen);
}So when I remove one of the enemies, I have to refresh the stack. Here's what I thought if let's say an enemy was removed from the index 'id': Code:
COUNT--; *sh[id] = *sh[COUNT]; delete sh[COUNT]; But apparently, virtual methods don't like getting moved around much and so I was stuck with the virtual methods from one enemy being assigned to the characteristics of another... Any idea on how to fix this? |
| 12-29-2008, 12:06 AM | #2 |
Code:
*sh[id] = *sh[COUNT]; You shouldn't be dereferencing here. Try this instead: Code:
sh[id] = sh[COUNT]; |
| 12-29-2008, 12:13 AM | #3 |
No, by doing that, I'd mess it all up: Code:
COUNT--; sh[id] = sh[COUNT]; delete sh[COUNT]; I tried without dereferencing and it crashed. |
| 12-29-2008, 12:21 AM | #4 |
You are right, I must have tunnel vision today. It should probably look something more like this: Code:
COUNT--; delete sh[id]; sh[id] = sh[COUNT]; I can't think how this dereferencing alone could account for this problem but it is almost certainly not what you want to do. EDIT: Now that I think about it, if the array is a ship* array and sizeof(enemy1) is different to sizeof(ship), then the problem may be that a partial copy results in the type data not being transferred. |
| 12-29-2008, 12:26 AM | #5 |
Hah! That worked! Thanks man I've been killing myself over this problem. I guess the solution was pretty simple after all. I was going looking over dynamic casts, RTTI and other stuff I don't understand (note I'm a beginner to this stuff). Anyway, thanks! :) |
| 12-30-2008, 02:39 AM | #6 |
I had no idea we had a programming forum here, that's awesome! Wait, how new are you to C++? Programming a game as a beginner is pretty ambitious. |
| 12-30-2008, 02:33 PM | #7 |
Well I know a bunch of programming structure and some of the basics. I just need to apply them. Trial and error by making a game seems to work quite well. :P |
| 12-30-2008, 06:47 PM | #8 |
Yes it does, it's how I got started. It wasn't until after i made my first tetris game that I looked back and actually read tutorials. :P And now i'm working with Craka_J as a C++ Programmer. |
