Enemy Player Detection


(Oli) #1

I use this code to make my enemy jump a certain direction if the “player” is at a certain range from the hitbox. However my problem is that it only works when the player is at that specific point. (hitbox + 100 or hitbox -100) Would anyone know to make a more general detection for my enemy. I tought of using a second hitbox as its detection but i dnt know how I would set it up thx.

the variable “unAcent” is a variable i setted up to be <= 100 . but that didnt work either :frowning:


(Jacob Albano) #2

Alright, so I finally have some time to respond to this.

Using a second hitbox is a good start, but the problem of course is that you can’t test collisions on specific masks that have been added to your entity with a Masklist. The approach I’d recommend in this case is to use a “sub-entity” with a separate hitbox and use that for detection.

Here’s a bare-bones example:

private var detection:Entity;

public function Enemy()
{
    // other stuff
    detection = new Entity();
    detection.width = 100;
    detection.height = 50;
    detection.centerOrigin();
}

override public function added():void
{
    world.add(detection);
}

override public function removed():void
{
    world.remove(detection);
}

You can now check for visibility like this:

if (detection.collide("Player", x - detection.halfWidth, y))
{
    // jump left
}

if (detection.collide("Player", x + detection.halfWidth, y))
{
    // jump right
}

You’ll want to do some other checks too; for example a call to world.collideLine() would help you find out if there’s a wall between the enemy and the player, but that’s the basic method I used recently.


(Oli) #3

thx alot I will look into that