Advertisement
RandomGuy32

move()

Feb 13th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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.
  3. 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.
  4. This means in order to keep my script on working I'd have to do some pretty unnecessary and unflexible adjustments.
  5. 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.
  6. */
  7.  
  8. function move() {
  9.         // An Wendepunkten Richtung umschalten
  10.         if ((moving.style.left == "50px") && (moving.style.top == "50px")) {
  11.             moveDirection = "forward";
  12.             moveAmountVertical = 1;
  13.             moveAmountHorizontal = 1;
  14.             }
  15.         if ((moving.style.left == "500px") && (moving.style.top == "300px")) {
  16.             moveDirection = "backward";
  17.             moveAmountVertical = 1;
  18.             moveAmountHorizontal = 1;
  19.             }
  20.         // Bewegungsrichtung vorwärts
  21.         if (moveDirection == "forward") {
  22.             // horizontale Bewegung bis zum Punkt left=500px
  23.             if (moving.style.left != "500px") {
  24.                 // neue Position festlegen
  25.                 moving.style.left = 50 + parseInt(moveAmountHorizontal) + "px";
  26.                 // moveAmountHorizontal um 1 erhöhen
  27.                 moveAmountHorizontal = parseInt(moveAmountHorizontal) + 1;
  28.                 }
  29.             // vertikale Bewegung bis zum Punkt top=300px
  30.             else if (moving.style.top != "300px") {
  31.                 // neue Position festlegen
  32.                 moving.style.top = 50 + parseInt(moveAmountVertical) + "px";
  33.                 // moveAmountVertical um 1 erhöhen
  34.                 moveAmountVertical = parseInt(moveAmountVertical) + 1;
  35.                 }
  36.             }
  37.         // Bewegungsrichtung rückwärts
  38.         if (moveDirection == "backward") {
  39.             // vertikale Bewegung bis zum Punkt top=50px
  40.             if (moving.style.top != "50px") {
  41.                 // neue Position festlegen
  42.                 moving.style.top = 300 - parseInt(moveAmountVertical) + "px";
  43.                 // moveAmountVertical um 1 erhöhen
  44.                 moveAmountVertical = parseInt(moveAmountVertical) + 1;
  45.                 }
  46.             // horizontale Bewegung bis zum Punkt left=50px
  47.             else if (moving.style.left != "50px") {
  48.                 // neue Position festlegen
  49.                 moving.style.left = 500 - parseInt(moveAmountHorizontal) + "px";
  50.                 // moveAmountHorizontal um 1 erhöhen
  51.                 moveAmountHorizontal = parseInt(moveAmountHorizontal) + 1;
  52.                 }
  53.             }
  54.         // jede Millisekunde wiederholen
  55.         window.setTimeout("move()", 1);
  56.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement