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

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