Picking Sprites

Picking

Sprites managed by either the sprite or packed manager can be selected, or picked, provided both the sprite and the manager are set to pickable. This is done as follows

mySpriteManager.isPickable = true;
mySpritePackedManager.isPickable = true;
mySprite.isPickable = true;

To do so, you need to:

  • Turn on picking on the sprites you want: sprite.isPickable = true;
  • Enable SpriteManager to support picking: spriteManager.isPickable = true;

To do picking you can use the scene.pickSprite:

var pickResult = scene.pickSprite(this.pointerX, this.pointerY);
if (pickResult.hit) {
pickResult.pickedSprite.angle += 0.5;
}

Pick and rotate selected sprite with sprite manager: Pick And Rotate A Selected Sprite Pick and rotate selected sprite with sprite packed manager: Pick and Rotate A Selected Sprite With Sprite Packed Manager

For performance reasons the default is to indicate a hit if the sprite is picked within its bounding rectangle. When you do not want a hit inside a transparent region of the sprite you need to add the following

mySprite.useAlphaForPicking = true;

and picking will only work if alpha > 0.5.

Transparent regions not reacting to pick: Transparent Regions No Picking

Where sprites are overlapping you can use multiPickSprite to get all the sprites under the mouse:

const pickResult = scene.multiPickSprite(this.pointerX, this.pointerY);
for (let i = 0; i < pickResult.length; i++) {
pickResult[i].pickedSprite.angle += Math.PI / 4;
}

multipicking of overlapping sprites: Multipicking Overlapping Sprites