I have been learning c++ and Allegro (graphics libary) for about a
year, and have been trying to get this code to work (in particular,
passing the array of classes), and when compiling i get the following errors
C:\Documents and Settings\User\Desktop\npc.h In member function `void cNPC::NPCmove(cNPC*)':
96 C:\Documents and Settings\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, int, short unsigned int&)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
107 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, short unsigned int&, int)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
118 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, int, short unsigned int&)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
129 C:\Documents and Settings\User\Desktop\npc.h no matching function for call to `cNPC::NPCmovecheck(cNPC**, short unsigned int&, int)'
note C:\Documents and Settings\User\Desktop\npc.h:66 candidates are: bool cNPC::NPCmovecheck(cNPC*, short unsigned int, short unsigned int)
I have tried passing the entire class (without the '&' and with '[10]')
NPCmovecheck(npcs[10],itsx-4,itsy)
and this works, but i would perfer to use a more efficient method.
If anyone can help it would be much appreciated.
class cNPC
{
public:
cNPC();
~cNPC();
unsigned short int getx(); // returns x
unsigned short int gety(); // returns y
void setx(unsigned short int newx);
void sety(unsigned short int newy);
char getimageID(); // returnes image id
void NPCmove(cNPC * npcs); // controls movement
bool NPCmovecheck(cNPC * npcs, unsigned short int xto, unsigned short int yto); //check if it can move //add xto yto
private:
unsigned short int itsx; // x co-ordinate
unsigned short int itsy; // y co-ordinate
char itsimageID; // image id for npc map
char itsdirection; // current direction
char itsframe; // current frame count [ 12 = stop moving]
bool itsmoving; // am i currently moving
bool itsspecial; // is the npc different - ID for special stuff
bool itsdoesmove; //does the npc move
};
cNPC::cNPC()
{
itsx = 128; // x co-ordinate
itsy = 128; // y co-ordinate
itsimageID = 0; // image id for npc map
itsdirection = 0; // current direction
itsframe = 0; // current frame count [ 12 = stop moving]
itsmoving = false; // am i currently moving
itsspecial = false; // is the npc different - ID for special stuff
itsdoesmove = true; //does the npc move
}
cNPC::~cNPC()
{
//nothing - deconstructor
}
void cNPC::setx(unsigned short int newx)
{
itsx = newx;
}
void cNPC::sety(unsigned short int newy)
{
itsy = newy;
}
unsigned short int cNPC::getx()
{
return itsx;
}
unsigned short int cNPC::gety()
{
return itsy;
}
//returnes true if no collision [ie. you can move]
bool cNPC::NPCmovecheck(cNPC * npcs, unsigned short int xto, unsigned short int yto)// add include xto yto
{
bool answer = true;
unsigned short int theirx;
unsigned short int theiry;
//for every obj check
for (char forx=0; forx < 10; forx ++)
{
theirx = npcs[forx].getx();
theiry = npcs[forx].gety();
if (xto < theirx + 32 && xto > theirx-32) //if x collision
{
if (yto < theiry + 32 && yto > theiry - 32) // if y collision
{
answer = false;
}
}
}
//map check NOT YET IMPLIMENTED
return answer;
}
//THE ERROR OCCURES IN THIS FUNCTION
void cNPC::NPCmove(cNPC * npcs)
{
if (itsdoesmove==true) // am i allowed to move
{
if (itsmoving==true) // am i still moving
{
switch (itsdirection)
{
case 0:
if (NPCmovecheck(&npcs,itsx+4,itsy)==true) // here
{
itsx += 4;
}
else //if cannot move
{
itsframe = 0;
itsmoving = false;
}
break;
case 1:
if (NPCmovecheck(&npcs,itsx,itsy-4)==true) // here
{
itsy -= 4;
}
else //if cannot move
{
itsframe = 0;
itsmoving = false;
}
break;
case 2:
if (NPCmovecheck(&npcs,itsx-4,itsy)==true) // here
{
itsx -= 4;
}
else //if cannot move
{
itsframe = 0;
itsmoving = false;
}
break;
case 3:
if (NPCmovecheck(&npcs,itsx,itsy+4)==true) // and here
{
itsy += 4;
}
else //if cannot move
{
itsframe = 0;
itsmoving = false;
}
break;
}
itsframe += 1;
if (itsframe == 12)
{
itsframe = 0;
itsmoving = false;
}
}
else //if not moving --- else if itsmoving == false
{
if (rand() % 50 == 0)
{
itsmoving = true;
itsframe = 0;
itsdirection = rand() % 4;
}
}
}
}
If anyone can help it would be much appreciated.