Hello, i am trying to get collision working with my npcs. I had it working with a single Npc but now i cant seem to get it working again.
Basically when the npc walks into the player i want it to stop so it doesnt walk all over the player. Sometimes it works sometimes it does not. Sometimes it will move the npc to a random spot, even though the old location doesn't equal that spot at all. Here is all relevant code please tell me why it only works some of the time.
Also it may only happen when the npc is on the lower half off my screen. But it may not. its hard to tell.
All the Npcs animated sprite numbers are the sprite + 200. So sprite 1 would be sprite number 201
Randomize movement only happens once at beginning of application start.
while (dd < 24)
{
++dd;
npcx[dd] = dbRND ( 434 );
npcx[dd] = dbRND ( 620 );
oldnpcx[dd] = npcx[dd];
oldnpcy[dd] = npcy[dd];
}
Create NPC (gets the data from mikenet)
dbCreateAnimatedSprite (npcloopnum + 200, (char *)npcfile[npcloopnum].c_str(), 6, 8, npcloopnum + 200);
// Hide it so Client can display it behind fringe
dbHideSprite(npcloopnum + 200);
Show Npc
/ Paste Sprite Here after hiding it so its behind fringe layer.
npcnum = 0;
while (npcnum < 21)
{
++npcnum;
dbPasteSprite ( npcnum + 200, npcx[npcnum], npcy[npcnum] ) ;
}
Npc Movement (random).
// NPC MOVEMENT VV
// 1 = Left
// 2 = right
// 3 = up
// 4 = down
// 5 = Left and Up
// 6 = Left and Down
// 7 = Right and Down
// 8 = Right and Up
npcnum = 0;
while(npcnum < 21)
{
npcnum++;
srand(time(0)); // Initialize random number generator.
npcmove[1] = dbRND ( 8 ) ;
npcmove[2] = dbRND ( 8 ) ;
npcmove[3] = dbRND ( 8 ) ;
npcmove[4] = dbRND ( 8 ) ;
npcmove[5] = dbRND ( 8 ) ;
npcmove[6] = dbRND ( 8 ) ;
npcmove[7] = dbRND ( 8 ) ;
npcmove[8] = dbRND ( 8 ) ;
npcmove[9] = dbRND ( 8 ) ;
npcmove[10] = dbRND ( 8 ) ;
npcmove[11] = dbRND ( 8 ) ;
npcmove[12] = dbRND ( 8 ) ;
npcmove[13] = dbRND ( 8 ) ;
npcmove[14] = dbRND ( 8 ) ;
npcmove[15] = dbRND ( 8 ) ;
npcmove[16] = dbRND ( 8 ) ;
npcmove[17] = dbRND ( 8 ) ;
npcmove[18] = dbRND ( 8 ) ;
npcmove[19] = dbRND ( 8 ) ;
npcmove[20] = dbRND ( 8 ) ;
if (npcmove[npcnum] == 1 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 1) dbSetSpriteFrame(npcnum + 200, 7);
npcdirection[npcnum] = 1;
npcx[npcnum]--;
dbPlaySprite ( npcnum + 200, 7, 12, AD );
}
if (npcmove[npcnum] == 2 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 2) dbSetSpriteFrame(npcnum + 200, 13);
npcdirection[npcnum] = 2;
npcx[npcnum]++;
dbPlaySprite ( npcnum + 200, 13, 18, AD );
}
if (npcmove[npcnum] == 3 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
if (npcdirection[npcnum] != 3) dbSetSpriteFrame(npcnum + 200, 19);
npcdirection[npcnum] = 3;
npcy[npcnum]--;
dbPlaySprite ( npcnum + 200, 19, 24, AD );
}
if (npcmove[npcnum] == 4 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
if (npcdirection[npcnum] != 4) dbSetSpriteFrame(npcnum + 200, 1);
npcdirection[npcnum] = 4;
npcy[npcnum]++;
dbPlaySprite ( npcnum + 200, 1, 6, AD );
}
if (npcmove[npcnum] == 5 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 5) dbSetSpriteFrame(npcnum + 200, 37);
npcdirection[npcnum] = 5;
npcy[npcnum]--;
npcx[npcnum]--;
dbPlaySprite ( npcnum + 200, 37, 42, AD );
}
if (npcmove[npcnum] == 6 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 6) dbSetSpriteFrame(npcnum + 200, 25);
npcdirection[npcnum] = 6;
npcy[npcnum]++;
npcx[npcnum]--;
dbPlaySprite ( npcnum + 200, 25, 30, AD );
}
if (npcmove[npcnum] == 7 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 7) dbSetSpriteFrame(npcnum + 200, 31);
npcdirection[npcnum] = 7;
npcy[npcnum]++;
npcx[npcnum]++;
dbPlaySprite ( npcnum + 200, 31, 36, AD );
}
if (npcmove[npcnum] == 8 && dbSpriteExist(npcnum + 200) == 1 && npccanmove[npcnum] == 1)
{
oldnpcy[npcnum] = npcy[npcnum];
oldnpcx[npcnum] = npcx[npcnum];
if (npcdirection[npcnum] != 8) dbSetSpriteFrame(npcnum + 200, 43);
npcdirection[npcnum] = 8;
npcy[npcnum]--;
npcx[npcnum]++;
dbPlaySprite ( npcnum + 200, 43, 48, AD );
}
// Cant move off gamescreen
if (npcx[npcnum] > 620 ) {npcx[npcnum] = 620; }
if (npcx[npcnum] < 0 ) {npcx[npcnum] = 0; }
if (npcy[npcnum] > 434 ) {npcy[npcnum] = 434; }
if (npcy[npcnum] < 0 ) {npcy[npcnum] = 0; }
dbSprite ( npcnum + 200, npcx[npcnum] , npcy[npcnum], npcnum + 200 );
}
// NPC MOVEMENT ^^
Collision Code!. My collision with NPCS works great. but not npc collision with me. Sometimes it works fine. Sometimes it puts them somewhere random.
npcnum = 0;
while (npcnum < 25)
{
++npcnum;
// Check collision with Player
if (dbSpriteCollision (npcnum + 200, Client ) == 1)
{
npcx[npcnum] = oldnpcx[npcnum];
npcy[npcnum] = oldnpcy[npcnum];
}
dbSprite ( npcnum + 200, npcx[npcnum] , npcy[npcnum], npcnum + 200 );
// Check collision with NPC
if (dbSpriteCollision (Client, npcnum + 200) == 1)
{
x = oldx;
y = oldy;
}
}
Sorry i know its alot of code. But i cant figure out whats causing the npcs to go somewhere random when i touch them. Then other times it seems to work fine. Could someone please attempt to figure it out.