Good morning, I have a student who would like to create code that chooses a random animal out of 30 different ones and make it operate so when the hatch button is clicked the egg opens and reveals an animal.
I’m thinking a hide element is involved and randomizing block?
This step explains how to have only one image on the screen at all times once hatched. This step is optional.
Set a variable check whenever the egg is hatched. If it’s hatched already, delete the past animal. Then if it’s not hatched yet, create a brand new one from in account for the animalRandom var.
onEvent("HatchClick", "click", function( ) {
if (hatched) { //if hatched, delete the previous random animal and hatched is set false
deleteElement(animalList[animalRandom].name);
hatched = false;
}
if (!hatched) { //if hatch false, randomize the animalRandom var and create an image based off the selected element's id and image link
animalRandom = randomNumber(0, 29);
hatched = true;
image(animalList[animalRandom].name, animalList[animalRandom].img);
}
});
You indicated the topic as being CSD unit 3 which would use Game Lab. Another way to possibly implement this would be to use 30 animations (graphics). If you name them with a number, you can assign a number to a random variable and assign that animation to be the costume or animation for that sprite.