Interactive animal (Main Menu) - UQcsse3200/2024-studio-2 GitHub Wiki
Interactive Animal
Interactive animal characters, such as an owl, that players can click on. When clicked, the animal will generate and display a random fun fact on the screen, adding an element of surprise and curiosity. At the same time, a sound will play, enhancing the experience and making the interaction more engaging. This feature will make the menu more dynamic and add a fun, interactive touch that players will enjoy exploring.
Setup for the owl facts
private void setupOwlFacts() {
owlFacts = new String[] {
// Animal-related facts
"A dog's nose print is as unique as a human fingerprint.",
"Crocodiles have been around for over 200 million years!",
"Some birds, like the Arctic Tern, migrate over 40,000 miles a year.",
"Dogs can understand up to 250 words and gestures.",
"Crocs can gallop on land like a horse for short bursts!",
"The owl can rotate its head 270 degrees without moving its body.",
"Dogs can smell diseases like cancer and diabetes!",
"A crocodile's bite is the strongest in the animal kingdom.",
"Parrots can mimic human speech better than any other animal.",
"A Greyhound can reach speeds of 45 mph!",
"The heart of a hummingbird beats over 1,200 times per minute!"
};
}
Positioning the owl and fact label
The updateOwlPosition
method ensures the owl stays at the bottom-right corner, and adjusts the size depending on fullscreen mode.
private void updateOwlPosition() {
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
if (Gdx.graphics.isFullscreen()) {
owlImage.setSize(300, 450); // Fullscreen size
} else {
owlImage.setSize(200, 300); // Windowed size
}
// Bottom-right position
owlImage.setPosition(screenWidth - owlImage.getWidth() - 20, 20);
// Position fact label near owl
factLabel.setPosition(screenWidth - 500, 130);
}
Adding the owl to the menu:
The addOwlToMenu
method adds the owl to the stage, sets up the label for displaying facts, and allows the owl to be clicked for playing sound and showing a random fact.
private void addOwlToMenu() {
owlImage.setPosition(1720, 150); // Initial position
owlImage.setSize(200,300); // Set size
stage.addActor(owlImage); // Add owl to stage
// Set up the fact label
factLabel = new Label("", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
factLabel.setPosition(1400, 130); // Position near the owl
factLabel.setFontScale(1f);
stage.addActor(factLabel);
// Owl click interaction
owlImage.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
owlSound.play(); // Play sound when clicked
String randomFact = owlFacts[MathUtils.random(0, owlFacts.length - 1)]; // Get random fact
factLabel.setText(randomFact); // Show fact
factLabel.addAction(Actions.sequence(
Actions.alpha(1), // Make label visible
Actions.delay(3), // Stay visible for 3 seconds
Actions.alpha(0, 1) // Fade out
));
}
});
}