I think something like this would be a more appropriate way to check for collisions, albeit there's more calculation going on:
PHP Code:
for (temp.p: players) {
// Determine width/height of player
temp.pw = 2;
temp.ph = 2;
// Find center of player
temp.px = p.x + .5 + pw/2;
temp.py = p.y + 1 + ph/2;
temp.tw = temp.th = 2;
temp.nx = this.x;
temp.ny = this.y;
// If NPC is not a showcharactor, try to find center
if (this.ani.name == null) {
temp.tw = this.width;
temp.th = this.height;
temp.nx = this.x + tw/2;
temp.ny = this.y + ty/2;
} else {
// If NPC is a showcharacter, find proper center of character
temp.tw = 2;
temp.th = 2;
temp.nx = this.x + .5 + tw/2;
temp.ny = this.y + 1 + ty/2;
}
// Check axis distance between NPC and player
temp.dx = abs(px - nx);
temp.dy = abs(py - ny);
// If distance on both axes are less than the width and height
// of the NPC and player combined, by half(because you're
// checking from the center of the entities), then a collision
// has occurred.
if (dx < (pw + nw)/2 && dy < (ph + nh)/2) {
// Collision!
}
}
The problem with the other method is you're relying on a single pixel collision within a box, instead of looking for the fact that two box entities are collision(checking the center of the player entering the entire size of the NPC isn't considering the fact the player has a size as well).
However I threw this up in notepad and didn't test it myself. Your mileage may vary.