# Randomizing Animations on a mouse click

**URL:** <https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291>\
**Category:** Coding and Debugging Help\
**Created:** [November 30, 2022, 9:07pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291 "2022-11-30T21:07:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aschoolland](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@aschoolland](https://forum.code.org/u/aschoolland)\
**Post date:** [November 30, 2022, 9:07pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/1 "2022-11-30T21:07:02Z")

</div>

**Link to the project or level:** ([Game Lab - Code.org](https://studio.code.org/projects/gamelab/dgBh-Rq6Llf5zX-0NZNA0GjNOIMg07R1-4y6rC4a1xA))

**What I expect to happen:** Student wants the dice animations to alternate randomly when a key is pressed.

**What actually happens:** Nothing, yet.

**What I’ve tried:** We considered using a random number block inside a set animation block, but that would not change the animation randomly.

---

<div class="post-metadata">

**Author:** ![edavis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/edavis/32/4927_2.png) [@edavis](https://forum.code.org/u/edavis)\
**Post date:** [December 1, 2022, 2:14am UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/2 "2022-12-01T02:14:00Z")

</div>

Unfortunately, the sharing permissions are not available on this link. You’ll need to update the sharing permissions in the class dashboard so we can see it. Sounds like a neat idea! Can’t wait to see it.

---

<div class="post-metadata">

**Author:** ![aschoolland](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@aschoolland](https://forum.code.org/u/aschoolland)\
**Post date:** [December 1, 2022, 4:55pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/3 "2022-12-01T16:55:36Z")

</div>

> **[Check out what I made](https://studio.code.org/projects/gamelab/6CPYgwMENiuEWvnUklurrCnCnHrjQ_DN0y9O7scba8k)**
>
> I wrote the code myself with Code.org

 ![Screen Shot 2022-12-01 at 7.53.43 AM](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/c/c2a8fb1afc3d9fb3a18c9e019835a33c20a52306.png)  
Sorry about that. Here is a screenshot of the code; since we can’t figure out how to randomize, the thumbprint won’t produce, so we can’t

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 1, 2022, 6:04pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/4 "2022-12-01T18:04:10Z")

</div>

@aschoolland,

Thanks for sharing your code … a few observations for you.

Do you want 6 different sprites or just 1 sprite that can have 6 different faces?

If you create a sprite and set its animation to be one of the six animations to begin with, the question then becomes, how do I randomly change the animation to one of the other 5 animations?

Here is some pseudocode that may help.

1. Rename all of your animations in the animation tab with a number (1, 2, 3, 4, 5 & 6).
2. Create a variable at the top of your code … something like “ **face** ”.
3. When you click “g” change the value of the variable to = a random number between 1 & 6.
4. Set the animation to equal **face**. – This part is actually tricky though because your animation names (1-6) will be “strings” (text) and the variable will be a number.
5. Here’s a little block of code that will fix that. What it does is change the variable “ **face** ” (a number) into a string (text) so it can match the names of your animations by adding a blank character to the end of the number. This is one way in javascript to get a string from a number.

![Screen Shot 2022-12-01 at 11.00.30 AM](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/b/bfc7f222f0f45fd1fe0c97238cf48d071f3575c3.png)

Have your student give it a shot and check back in if it still isn’t working quite right.

Good luck!

Mike

---

<div class="post-metadata">

**Author:** ![aschoolland](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@aschoolland](https://forum.code.org/u/aschoolland)\
**Post date:** [December 2, 2022, 8:32pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/7 "2022-12-02T20:32:54Z")

</div>

Thanks for the tips, Mike!  
Here’s what we’ve tried. What do you suggest?

> **[Check out what I made](https://studio.code.org/projects/gamelab/QtL7q3-8TdesYuqWYOPVxZJLP39C9U90J-wDppH7DXs)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [December 2, 2022, 10:55pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/8 "2022-12-02T22:55:11Z")

</div>

@aschoolland,

This looks great! You are only one small step away from it working. On line 6 of your code, the variable should be called “face”. Dice is your sprite and Face would be the number represented on the dice.

If you make that one change, I think it will work!

Mike

---

<div class="post-metadata">

**Author:** ![aschoolland](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@aschoolland](https://forum.code.org/u/aschoolland)\
**Post date:** [December 2, 2022, 11:25pm UTC](https://forum.code.org/t/randomizing-animations-on-a-mouse-click/37291/9 "2022-12-02T23:25:51Z")

</div>

That worked great! Thank you SO much!
