Crafty.findClosestEntityByComponent

public this .findClosestEntityByComponent(String comp, Number x, Number y[, Object target])

Finds closest entity with certain component at given coordinates.

comp

Component name

x

x position where to look for entities

y

y position where to look for entities

target

Target element wherein to look for entities

This method is used internally by the .mouseDispatch and .touchDispatch methods, but can be used otherwise for Canvas entities.

Finds the top most entity (with the highest z) with a given component at a given point (x, y). For having a detection area specified for the enity, add the AreaMap component to the entity expected to be found.

The 'target' argument is only meant to be used by .mouseDispatch and touchDispatch; defaults to Crafty.stage.elem, thus using this function directly is only worth anything for canvas entities.

Returns the found entity, or undefined if no entity was found.

Example

var coords = { x: 455, y: 267 },
    closestText = Crafty.findClosestEntityByComponent("Text", coords.x, coords.y);