home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Javascript / InteractiveWebDesignJavascript / Scripting / pokemon / netscapeFix / scripts / NNsprites.js
Encoding:
JavaScript  |  2001-07-18  |  5.8 KB  |  169 lines

  1.  
  2. /*
  3.     ALL CODE WRITTEN BY HAND BY: ANDRE KING
  4.     DATE: 5/8/2000
  5.     
  6.     PLEASE, FEEL FREE TO USE ANY CODE HEREIN, 
  7.     BUT I WOULD APPRECIATE AN EMAIL TO 
  8.         andrewtek@excite.com
  9.     LETTING ME KNOW JUST HOW MUCH OF MY CODE IS OUT THERE
  10.  
  11. */
  12.  
  13. /*what browser is this? */
  14. var usingNetscape = navigator.appName=="Netscape";
  15.  
  16. if(usingNetscape){
  17.     document.captureEvents(Event.MOUSEMOVE);
  18.     document.onMouseMove = mouseTrackerNN;
  19. }else{
  20.     document.onmousemove = mouseTrackerIE;
  21. }
  22.  
  23. //if (document.all&&window.print)
  24. //    document.body.style.cssText="overflow-x:hidden;overflow-y:scroll"
  25.  
  26. //this holds all the sprites in it
  27. var spriteVector = new Array()
  28.  
  29. var twoPI = Math.PI * 2
  30. var mouseX = 0, mouseY = 0;
  31.  
  32. //same as the start() and run() methods of JAVA (Oh, what a language)
  33. function startAnimation(){
  34.     setInterval('moveSprites()', 30);
  35. }
  36.  
  37. function addSprite(name, width, height, maxVel, minVel){
  38.     var spriteName = new String(name)
  39.     if(spriteName.indexOf(".") != -1)
  40.         alert("Sprite's Name Should Not Contain File Extension: " + name)
  41.     else{
  42.         xStart = Number(150)
  43.         yStart = Number(150)
  44. //        alert(xStart + ", " + yStart)
  45. //        document.writeln("<div id=\"directional" + name + "\" style=\"position: absolute;\"><img src=\"images/direction.gif\"></div>")
  46. //        document.writeln("<div id=\"" + name + "\" style=\"position: absolute;\"><img name=\"" + name + "img\" src=\"images/"+name+".gif\" width=" + width+" height=" + height+"\"></div>")
  47.         spriteVector[spriteVector.length] = new sprite(name, width, height, xStart, yStart, maxVel, minVel) 
  48.     }
  49. }
  50.  
  51. /*A Sprite Object*/
  52. function sprite(name, width, height, xPos, yPos, maxVel, minVel){
  53.     this.name = name;                        //name referring to div/layer tag
  54.     this.width = width;                        //width of image
  55.     this.height = height;                    //height of image
  56.     this.xPos = xPos;                        //current x Position
  57.     this.yPos = yPos;                        //current y Position
  58.     this.xOld = xPos;                        //previous x Position
  59.     this.yOld = yPos;                        //previous y Position
  60.     this.maxVel = maxVel;                    //maximum speed of this sprite
  61.     this.minVel = minVel;                    //minimum speed of this sprite
  62.     this.actualVelocity = minVel;            //current speed of this sprite
  63.     if(usingNetscape){
  64.         this.domObj = eval("document.sprite" + spriteVector.length)
  65.         this.directional = eval("document.directional"+spriteVector.length)
  66.     }else{
  67.         this.domObj = eval("sprite" + spriteVector.length +".style")
  68.         this.directional = eval("directional"+ spriteVector.length +".style")
  69.     }
  70.     this.currAngle = 0
  71. }
  72.  
  73. /*Mouse Tracking Functions... used to find the exact position of the mouse.*/
  74. function mouseTrackerIE(){
  75. //    mouseX = 150;
  76. //    mouseY = 150;      
  77.     mouseX = window.event.x + document.body.scrollLeft;
  78.     mouseY = window.event.y + document.body.scrollTop;      
  79. }
  80. function mouseTrackerNN(mouse){
  81. //    mouseX = 150;
  82. //    mouseY = 150;      
  83.     mouseX = mouse.x;
  84.     mouseY = mouse.y;
  85. }
  86.  
  87. //sprite movement functiuons 
  88. function moveSprites(){
  89.     //code for moving sprites
  90.     var curSpr
  91.     for(var i = 0; i < spriteVector.length; i++){
  92.         curSpr = spriteVector[i]                //easier to type than sprite[i]
  93.         //check if the sprite is within range
  94.         if(getDistance(curSpr.xPos, curSpr.yPos, mouseX, mouseY) > 25){
  95.             moveSpriteToMouse(curSpr)
  96.         }else{
  97.             moveSpriteInCircles(curSpr)
  98.         }
  99.         makeSpriteMovement(curSpr)
  100.     }
  101. }function moveSpriteToMouse(curSpr){
  102.     curSpr.xOld = curSpr.xPos;
  103.     curSpr.yOld = curSpr.yPos;
  104.     actualAngle = getAngle(curSpr.xPos, curSpr.yPos, mouseX, mouseY)
  105.     var diffToZero = curSpr.currAngle
  106.     var actualAngleMoved = actualAngle - diffToZero
  107.     //code determins which direction to go towards mouse
  108.     if(actualAngleMoved < -Math.PI)
  109.         actualAngleMoved += twoPI
  110.     else if(actualAngleMoved > Math.PI)
  111.         actualAngleMoved -= twoPI
  112.     if(Math.abs(actualAngleMoved) > (curSpr.minVel/50) * 2){
  113.         if(actualAngleMoved < 0){
  114.             curSpr.currAngle -= curSpr.minVel/50
  115.             if (curSpr.currAngle < -Math.PI)curSpr.currAngle+=twoPI
  116.         }
  117.         else{
  118.             curSpr.currAngle += curSpr.minVel/50
  119.             if (curSpr.currAngle > Math.PI)curSpr.currAngle-=twoPI
  120.         }
  121.     }
  122. }function moveSpriteInCircles(curSpr){
  123.     curSpr.currAngle += curSpr.minVel/50
  124. }function makeSpriteMovement(curSpr){//move sprites based on internally changed values
  125.     newXY = movePointOnVector(curSpr.minVel, curSpr.currAngle, curSpr.xPos, curSpr.yPos)
  126.     curSpr.xPos = newXY[0];
  127.     curSpr.yPos = newXY[1];
  128.     directionalXY = movePointOnVector(curSpr.width * .75, curSpr.currAngle, newXY[0], newXY[1])
  129.     curSpr.domObj.left = curSpr.xPos - (curSpr.width / 2);
  130.     curSpr.domObj.top = curSpr.yPos - (curSpr.height /2);
  131.     curSpr.directional.left = directionalXY[0] - (7.5);
  132.     curSpr.directional.top = directionalXY[1] - (7.5);
  133. }
  134. /*X, Y Manipulation Functions*/
  135. function getAngle(startX, startY, endX, endY){
  136.     var x = startX - endX;                //difference between x's (abs is not necessary since values will be squared)
  137.     var y = startY - endY;                //difference between y's (abs is not necessary since values will be squared)
  138.     var newAngle = -Math.atan2(x, y)+Math.PI;
  139.     return -newAngle;
  140. }//distance between two points
  141. function getDistance(startX, startY, endX, endY){    
  142.     xDist = startX - endX;
  143.     yDist = startY - endY;
  144.     return Math.sqrt((xDist * xDist) + (yDist * yDist))
  145. }//moves a point in a particular direction keeping actual velocity
  146. function movePointOnVector(velocity, angle, currX, currY){
  147.     var cos = Math.cos(angle);
  148.     var sin = Math.sin(angle);
  149.     return returnVars = new Array(currX + velocity * sin, currY + velocity * cos);
  150. }
  151. //adds disclaimer to window
  152. function startDisclaimer(disclaimerText){
  153.     setTimeout("disclaimerMoveUp(0)", 5000)
  154. }
  155. function disclaimerMoveUp(factor){
  156.     var fctr = factor
  157.     fctr -= 1.75        
  158.     if(usingNetscape)
  159.         document.disclaimer.top = (15 + fctr)
  160.     else
  161.         disclaimer.style.top = (15 + fctr)
  162.     if(fctr > -100)
  163.         setTimeout("disclaimerMoveUp(" + fctr + ")", 20)
  164. }
  165.  
  166.  
  167. //add sprites and disclaimer
  168.  
  169. startDisclaimer()