I have a concept for an interactive bug animation. My idea involves having five bug images fly in from the side of the screen, bounce around randomly, and bounce off the edges. Each bug should have a unique starting position and direction.
To kick things off, I've set up some global variables:
var flyVar;
var flySpeed = 5;
var widthMax = 0;
var heightMax = 0;
var xPosition = 0;
var yPosition = 0;
var xDirection = "";
var yDirection = "";
var bugFly;
var count = 1;
var bug = "bug";
A function called setBugs()
is responsible for determining the screen dimensions and adjusting the parameters accordingly.
I've also created a function called bugStartingPlace, which specifies the starting position and direction for each bug. While I won't go into all the details, the function assigns unique values to "bug1" through "bug5".
function bugStartingPlace(bugName) {
//Accepts string as argument and sets the starting position and starting direction of each bug.
if (bugName == "bug1") {
xPosition = 0;
yPosition = 100;
xDirection = "right";
yDirection = "up";
}
}
For the animation itself, the flyBug() function is in charge. It sets the position of the bug image based on various conditions. While this function works for a single bug, the challenge lies in adapting it for five bugs.
function flyBug() {
if (xDirection == "right" && xPosition > (widthMax - document.getElementById("bugImage").width - flySpeed))
xDirection = "left";
<!--More flow control statements are here-->
document.getElementById("bug1").style.left = xPosition + "px";
document.getElementById("bug1").style.top = yPosition + "px";
<!-- More statements are here that set the position of the image -->
}
So, the challenge now is getting the animation to start using the body onload() event. Since setInterval doesn't support functions with parameters, I've implemented a workaround using the count variable. This approach adds complexity, as I need to adjust both the count and bug names to utilize the functions effectively.
While the current setup works for a single bug, it lacks efficiency for managing multiple bugs. I welcome any insights or suggestions on how to improve the program logic.