Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Okay, what I've got is an HTML object called "moving" which is supposed to move in a certain way when the function move() is called. It works pretty fine, but it is a performance whore, probably because the script is run anew every millisecond. Now I've tried to decrease the frequency of updates, which of course means I had to increase the amount of pixels the object moves every turn.
- This, however, broke the whole script because the object changes its direction of movement only when it reaches one certain point. But since the CSS attributes "top" and "left" always contain "px" at the end, I can only check whether they have a certain value or not, but I can't compare them against anything. Now that the degree of movement has changed, the object never reaches the control value and therefore moves to the right infinitely.
- This means in order to keep my script on working I'd have to do some pretty unnecessary and unflexible adjustments.
- There are probably solutions out there on the Internet, but I tried to come up with my own ideas on how to accomplish moving HTML objects.
- */
- function move() {
- // An Wendepunkten Richtung umschalten
- if ((moving.style.left == "50px") && (moving.style.top == "50px")) {
- moveDirection = "forward";
- moveAmountVertical = 1;
- moveAmountHorizontal = 1;
- }
- if ((moving.style.left == "500px") && (moving.style.top == "300px")) {
- moveDirection = "backward";
- moveAmountVertical = 1;
- moveAmountHorizontal = 1;
- }
- // Bewegungsrichtung vorwärts
- if (moveDirection == "forward") {
- // horizontale Bewegung bis zum Punkt left=500px
- if (moving.style.left != "500px") {
- // neue Position festlegen
- moving.style.left = 50 + parseInt(moveAmountHorizontal) + "px";
- // moveAmountHorizontal um 1 erhöhen
- moveAmountHorizontal = parseInt(moveAmountHorizontal) + 1;
- }
- // vertikale Bewegung bis zum Punkt top=300px
- else if (moving.style.top != "300px") {
- // neue Position festlegen
- moving.style.top = 50 + parseInt(moveAmountVertical) + "px";
- // moveAmountVertical um 1 erhöhen
- moveAmountVertical = parseInt(moveAmountVertical) + 1;
- }
- }
- // Bewegungsrichtung rückwärts
- if (moveDirection == "backward") {
- // vertikale Bewegung bis zum Punkt top=50px
- if (moving.style.top != "50px") {
- // neue Position festlegen
- moving.style.top = 300 - parseInt(moveAmountVertical) + "px";
- // moveAmountVertical um 1 erhöhen
- moveAmountVertical = parseInt(moveAmountVertical) + 1;
- }
- // horizontale Bewegung bis zum Punkt left=50px
- else if (moving.style.left != "50px") {
- // neue Position festlegen
- moving.style.left = 500 - parseInt(moveAmountHorizontal) + "px";
- // moveAmountHorizontal um 1 erhöhen
- moveAmountHorizontal = parseInt(moveAmountHorizontal) + 1;
- }
- }
- // jede Millisekunde wiederholen
- window.setTimeout("move()", 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement