Use LIST to store random animations for scene items

I want to have the people in my scene perform random animations - can I do this with a list? I have a list of people and made a function called “randomMovement” with a screen item parameter “target” - I can use the Move block to move “target”, but I would like it to perform a random animation instead of moving, but I can’t figure out a way.

Hi @demetere,

The play animation CoBlock doesn’t take variables. So if you want to play random animations from the list, you can use TypeScript:

let objects = [“Casual girl”, “Casual boy”];
let animation = [“Run”, “Skip”];

let objName = objects[Math.floor(Math.random()*objects.length)];
let animationName = animation[Math.floor(Math.random()*animation.length )];

(Scene.getItem(objName) as AnimatedItem).animation.play(animationName)

Feel free to remix this example and explore the code:

@Nikolay, is there a way to get a list of the animations available for an Item, in the API? (I searched for this, but couldn’t find any publicly-exposed method).

There is no such method at the moment. I will add it to our feedback board.

Hello, I am trying to make object Dreidel randomly fall on different sides after spinning… There are animations for it [Put one in, Take half, etc], but I cannot figure out how to code to randomize it. Is there a way to do it?
Thank you!

Hi @mvoskoboynik,

If you’re using CoBlocks, you can’t currently use variables/lists for animations; however, we can generate a random number, associate that number with an animation, and do a conditional check on the number:

If you’re using Javascript/Typescript, then we can use an array/list to store that relationship:

let dreidel = Scene.getItem("Dreidel") as AnimatedItem;
let animations = ['Take half','Put one in','Take all'];
let randNum = Math.round( Math.random() * (animations.length - 1) )

dreidel.input.onClick( function(){
    dreidel.animation.play( animations[ randNum ] )
})

See this remixable example:

Hope that helps! Let me know if you have any further questions.

Geoff @ TechLeap

Thank you so much Geoff,

It worked! I used the CoBlocks option.

Appreciate your help!

MV

1 Like