Check if the player enters an object perimeter [SOLVED]


(Zouhair Elamrani Abou Elassad) #1

Hi,

I was wondering if there is a way to check if something (Player) enters a perimeter of another object, just like having a circular hitbox or a rounded mask, thank you.


(Martí Angelats i Ribera) #2

Theres always a way. :stuck_out_tongue:

The simplest way is to use a pixelmask.

var bitmapData:BitmapData = new BitmapData(2*RADIUS, 2*RADIUS, true, 0);
Draw.setTarget(bitmapData);
Draw.circle(RADIUS, RADIUS, RADIUS);

var pixelmask:Pixelmask = new Pixelmask(bitmapData);

mine.mask = pixelmask;

Obviously, changle RADIUSfor your radius. If you want to use the exact same radius multiple times, use the same pixelmask.


(Zouhair Elamrani Abou Elassad) #3

That’s a good way to put it, i also went for Draw.circlePlus to be able to set the fill attribute ^^.

Thanks man.


(Martí Angelats i Ribera) #4

Oh true. Forgot about that.

Well i’m glad it helped.


(Mike Evmm) #5

Although @Copying’s solution is much more efficient, in case you can’t use a pixelmask you can always check the actual distance.

if(FP.distance(object.x, object.y, player.x, player.y) < RADIUS) {
    //Do stuff
}

(Jacob Albano) #7

@Copying’s solution is absolutely not more efficient. Your way is much better.


(Martí Angelats i Ribera) #8

Actually there the improved version should be:

public static function insideRadius(objectX:Number, objectY:Number, playerX:Number, playerX:Number, radius:Number):Boolean
{
    var x:Number = playerX - objectX;
    var y:Number = playerY - objectY;
    return (x * x + y * y)  <  (radius * radius);
}

This solution is more efficient but ignores the player mask and it’s a distance to a fixed point.


(Zouhair Elamrani Abou Elassad) #9

… That’s a great idea, i didn’t know about the FP.distance, that’s very straight forward :), the same thing about the use of the Pythagore equation ^^, thanks guys.