[SDL] Enemy class inheriting from tile class.
I'm trying to learn how to make an enemy class inherit from a tile class to use with a level editor that places tiles. However, I'm still very new to programming, and I probably don't know how to use inheritance correctly.
Here is the code:
Code:
class tile
{
private:
SDL_Rect box;
int type;
public:
tile( int x , int y , int ttype );
void show();
int gettype();
SDL_Rect getbox();
};
class enemy : public tile
{
private:
SDL_Rect box;
int type;
public:
enemy( int x , int y , int ttype ) : tile( x , y , ttype ) {}
void move( tile * tiles );
void show( tile * tiles );
};
...
void enemy::move( tile * tiles )
{
int velx = 0;
int vely = 0;
vely += gravity;
velx += 10;
box.y += vely;
box.x += velx;
if( col( box , &tiles ) == true )
{
box.x -= velx;
box.y -= vely;
}
}
void enemy::show( tile * tiles )
{
if( col( camera , box ) == true )
{
apply( box.x , box.y , enemy , screen );
}
move( tiles );
}
...In the editor...:
Code:
bool settiles( tile * tiles[] )
{
...
if( ( ttype >= 0 ) && ( ttype < PLATFORM ) )
{
tiles[t] = new tile( x , y , ttype );
}
else if( ttype == ENEMY )
{
tiles[t] = new enemy( x , y , ttype );
}
...
...And in the main loop:
Code:
int main( int argc , char * argv[] )
{
bool quit = false;
tile * tiles[TT];
...
while( quit == false )
{
...
for( int t = 0 ; t < TT ; t++ )
{
tiles[t]->show();
tiles[t]->enemy::show( tiles[t] );
}
...
I have tried many different approaches, but I can't seem to get it right.
This particular approach gives me this error at the main loop:
Code:
error: 'enemy' is not a base of 'tile'
Can anyone give me some advice on how to get this right?
I'm using LazyFoo's tutorials, by the way.
Thank you in advance! :-)