Action Script & Tutorials 26 May 2006 03:23 am

MovieClip point 2 point mover class

(Visited 1478 times)

This class is based on Colin Moock Follow Grid of Point script.
With this class you can generate movement of movieclip from an array that contain some coordinat points.

Actionscript:
  1. //
  2. // save it as moveToPoint.as in janumedia folder
  3. //
  4. class janumedia.moveToPoint {
  5.    
  6.     private var pathArray:Array;
  7.     private var mc:MovieClip;
  8.    
  9.     function moveToPoint(t:MovieClip,pathTo:Array,speed:Number){
  10.        
  11.         pathArray   = pathTo;
  12.         drawPath(t,pathArray);
  13.         mc      = t;
  14.         mc.count    = 0;
  15.         mc.oldT  = getTimer();
  16.         mc.speed    = speed;
  17.         mc.oriX  = mc._x;
  18.         mc.oriY  = mc._y;
  19.         mc.xPos  = pathTo[mc.count].x;
  20.         mc.yPos  = pathTo[mc.count].y;
  21.         mc.owner    = this;
  22.         mc.onEnterFrame = function(){
  23.             this.owner.doMove();
  24.         }
  25.        
  26.     }
  27.    
  28.     private function doMove(){
  29.         mc.curT = getTimer();
  30.         mc.elapsedSeconds = (mc.curT - mc.oldT) / 1000;
  31.         mc.oldT = mc.curT;
  32.         mc.moveDist = mc.speed * mc.elapsedSeconds;
  33.         // Determine distance between clip and destination
  34.         var deltaX = mc._x - mc.xPos;
  35.         var deltaY = mc._y - mc.yPos;
  36.         var dist = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
  37.    
  38.         if (mc.moveDist>= dist) {
  39.             mc._x = mc.xPos;
  40.             mc._y = mc.yPos;
  41.             mc.count++;
  42.             if(mc.count <pathArray.length){
  43.                 mc.xPos  = pathArray[mc.count].x;
  44.                 mc.yPos  = pathArray[mc.count].y;
  45.             } else {
  46.                 delete mc.onEnterFrame;
  47.             }
  48.        
  49.         } else {
  50.             // Allocate speed between x and y axes
  51.             var moveX = mc.moveDist * (deltaX / dist);
  52.             var moveY = mc.moveDist * (deltaY / dist);
  53.             // Move follower towards the destination.
  54.             mc._x -= moveX;
  55.             mc._y -= moveY;
  56.         }
  57.     }
  58.    
  59.     private function drawPath(t:MovieClip,data:Array){
  60.         var path = t._parent.createEmptyMovieClip("mcpath",t._parent.getNextHighestDepth(),{_x:0,_y:0});
  61.         with(path){
  62.             clear();
  63.             lineStyle(0, 0x000000, 50);
  64.             moveTo(t._x, t._y);
  65.         }
  66.         for(var i=0 ; i<data.length ; i++){
  67.             path.lineTo(data[i].x, data[i].y);
  68.         }
  69.     }
  70. }

Don't forget to download the sample here

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply

You must be logged in to post a comment.