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 perform picking, you can use 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. If 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
When sprites overlap, 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