home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / msdn / vc6intro / ieref.chm / inet401 / samples / dhtml / dhtmlpath / htmlpath.js next >
Encoding:
Text File  |  1998-06-12  |  2.3 KB  |  127 lines

  1. var tickDuration;
  2. tickDuration = 50;
  3.  
  4. var activeObjectCount;
  5. var activeObjects;
  6. var itemDeactivated;
  7.  
  8. var tickGeneration;
  9.  
  10. activeObjects = new Array();
  11. activeObjectCount = 0;
  12. timerRefcount = 0;
  13. itemDeactivated = false;
  14.  
  15. tickGeneration = 0;
  16.  
  17. function initializePath(e) {
  18.  e.waypointX = new Array();
  19.  e.waypointY = new Array();
  20.  e.duration = new Array();
  21.  
  22. }
  23.  
  24. function addWaypoint(e, number, x, y, duration) {
  25.  e.waypointX[number] = x;
  26.  e.waypointY[number] = y;
  27.  e.duration[number] = duration;
  28. }
  29.  
  30. function compact() {
  31.   var i, n, c;
  32.  
  33.   n = new Array();
  34.   c = 0;
  35.   itemDeactivated = false;
  36.   for (i=0; i<activeObjectCount; i++)  {
  37.      if (activeObjects[i].active == true) {
  38.         n[c] = activeObjects[i];
  39.         c++;
  40.      }
  41.   }
  42.  
  43.   activeObjects = n;
  44.   activeObjectCount = c;
  45. }
  46.  
  47. function tick(generation) {
  48.  
  49.   if (generation < tickGeneration) {
  50.     // alert("Error "+generation);
  51.      return;
  52.   }
  53.  
  54.   //alert("tick: "+generation);
  55.  
  56.   if (itemDeactivated)
  57.      compact();
  58.  
  59.   if (activeObjectCount == 0) {
  60.      return;
  61.   }
  62.   else {
  63.     for (i=0; i<activeObjectCount; i++) {
  64.       moveElement(activeObjects[i]);
  65.     }
  66.  
  67.     window.setTimeout("tick("+generation+");", tickDuration);
  68.   }
  69. }
  70.  
  71. function start(e) {
  72.   if (itemDeactivated)
  73.      compact();
  74.  
  75.   activeObjects[activeObjectCount] = e;
  76.   activeObjectCount++;
  77.  
  78.   if (activeObjectCount == 1) { 
  79.     tickGeneration++;
  80.     tick(tickGeneration);
  81.   }
  82. }
  83.  
  84. function runWaypoint(e, startPoint, endPoint) {
  85.  
  86.   var startX, startY, endX, endY, duration;
  87.  
  88.   if (e.waypointX == null)  
  89.     return;   
  90.  
  91.   startX = e.waypointX[startPoint];
  92.   startY = e.waypointY[startPoint];
  93.   endX = e.waypointX[endPoint];
  94.   endY = e.waypointY[endPoint];
  95.  
  96.   duration = e.duration[endPoint];
  97.   e.ticks = duration / tickDuration;
  98.  
  99.   e.endPoint = endPoint;
  100.   e.active = true;
  101.   e.currTick = 0;
  102.  
  103.   e.dx = (endX - startX) / e.ticks;
  104.   e.dy = (endY - startY) / e.ticks;
  105.  
  106.   e.style.posLeft = startX;
  107.   e.style.posTop = startY;
  108.  
  109.   start(e);
  110. }  
  111.  
  112. function moveElement(e) {
  113.   e.style.posLeft += e.dx;
  114.   e.style.posTop += e.dy;
  115.  
  116.   e.currTick++;
  117.  
  118.   if (e.currTick > e.ticks) {
  119.     e.active = false;
  120.     itemDeactivated = true;
  121.     if (e.onpathcomplete != null) {
  122.        window.pathElement = e;
  123.        e.onpathcomplete()
  124.     }
  125.   }
  126. }
  127.