Randomizing Animations on a mouse click

Link to the project or level: (Game Lab - Code.org)

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.

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.


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

@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

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

Good luck!

Mike

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

@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

That worked great! Thank you SO much!