Phaser API Documentation

  Version: 
makePixelPerfect([alphaTolerance])

Description:

Creates a function that can be passed to setInteractive, enable or setHitArea that will handle pixel-perfect input detection on an Image or Sprite based Game Object, or any custom class that extends them.

The following will create a sprite that is clickable on any pixel that has an alpha value >= 1.

this.add.sprite(x, y, key).setInteractive(this.input.makePixelPerfect());

The following will create a sprite that is clickable on any pixel that has an alpha value >= 150.

this.add.sprite(x, y, key).setInteractive(this.input.makePixelPerfect(150));

Once you have made an Interactive Object pixel perfect it impacts all input related events for it: down, up, dragstart, drag, etc.

As a pointer interacts with the Game Object it will constantly poll the texture, extracting a single pixel from the given coordinates and checking its color values. This is an expensive process, so should only be enabled on Game Objects that really need it.

You cannot make non-texture based Game Objects pixel perfect. So this will not work on Graphics, BitmapText, Render Textures, Text, Tilemaps, Containers or Particles.

Parameters:

name type arguments Default description
alphaTolerance number <optional> 1

The alpha level that the pixel should be above to be included as a successful interaction.

Returns:
Description:

A Pixel Perfect Handler for use as a hitArea shape callback.

Type:
  • function
Examples:
Since: 3.10.0