Multipe Sprites actions

Hello guys , i need to apply differents actions to differents Sprites , but in the documentation there is no informations to control each Sprites , i use this code :

var spriteManagerPlayer = new BABYLON.SpriteManager(“playerManager”, “1.png”, 2, {width: 250,height: 250}, scene);
var player = new BABYLON.Sprite(“player”, spriteManagerPlayer);

var spriteManagerPlayer1 = new BABYLON.SpriteManager(“playerManager1”, “2.png”, 2, {width: 250,height: 250}, scene);
var player1 = new BABYLON.Sprite(“playe1r”, spriteManagerPlayer1);

spriteManagerPlayer1.isPickable = true;
spriteManagerPlayer.isPickable = true;

scene.onPointerDown = function (evt) {
var pickResult = scene.pickSprite(this.pointerX, this.pointerY);
if (pickResult.hit) { player1.size = 0; }
};

we can see that scene.onPointerDown is affecting player1 and player2 , i want to have different action when i click player1, and another action when i click player2

Many Thanks

Hey!

what about using pickResult.pickedSprite ? This shoudl give you the picked sprite so you can adapt your code based on that?

Example: https://www.babylonjs-playground.com/#9RI8CG#0

Here’s what i think i’d do;
https://www.babylonjs-playground.com/#9RI8CG#58

BABYLON.Sprite.prototype.onPick = function(){
    this.angle += 0.5; // Default 'onPick' behavior, if any. else leave empty function.
};

// .. Specific behavior / function for "player" sprite
player.onPick = function(){
    player.size = 1;
};

// .. Specific behavior / function for "player2" sprite
player2.onPick = function(){
    player2.size += 0.25;
};

// onPointerDown..
if (pickResult.hit && pickResult.pickedSprite.onPick) {
    pickResult.pickedSprite.onPick();
}

// etc, you get the point :)
1 Like

thanks man that what i need (y) , just a simple question how you know that ? i mean i checked the whole documentation and i dont found .onPick function ??

1 Like

This function does not exist it was added by aWeirdo. The key point is the use of pickResult.pickedSprite

1 Like

I hope i can add this link without it being taken the wrong way,

It’s a great source for information on anything javascript :slight_smile:

1 Like