vendredi 26 juin 2015

call a function multiple times without conflict in actionscript

I have some dots on stage and want to connect them with lines . i use this function to Animate drawing of lines . i found it here

var fromX:Number;
var fromY:Number;
var toX:Number;
var toY:Number;
var dx:Number;
var dy:Number;
var increment:Number;
var origToX:Number;
var origToY:Number;
var origDiffX:Number;
var origDiffY:Number;
var pDiffX:Number;
var pDiffY:Number;
var multiplier:Number;

function drawLineProgressively(fromX:Number, fromY:Number, toX:Number, toY:Number, multiplier:Number):void
{
      trace("Public function drawLineProgressively called.")
      this.fromX = fromX;
      this.fromY = fromY;
      this.toX = toX;
      this.toY = toY;
      origToX = toX;
      origToY = toY;
      dx = toX - fromX;
      dy = toY - fromY;
      increment = Math.max(Math.abs(dx), Math.abs(dy));
      increment = Math.round(increment / multiplier);
      dx /= increment;
      dy /= increment;
      graphics.lineStyle(1, 0Xff0000)
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(e:Event):void
{
      var tx:Number;
      var ty:Number;
      trace("Adding:", dx, "to x.");
      trace("Adding:", dy, "to y.");
      tx = fromX + dx;
      ty = fromY + dy;
      origDiffX = Math.abs(tx - origToX);
      origDiffY = Math.abs(ty - origToY);
      origDiffX = Math.round(origDiffX);
      origDiffY = Math.round(origDiffY);
      trace("Orig DiffX:", origDiffX);
      trace("Orig DiffY:", origDiffY);
      if ((pDiffX > origDiffX) || (pDiffY > origDiffY) || (!pDiffX) || (!pDiffY))
      {
              trace("true 2");
              trace("Drawing from: ", fromX, ",", fromY, "to:, ", tx, ",", ty);
              graphics.moveTo(fromX, fromY);
              graphics.lineTo(tx, ty);
              pDiffX = origDiffX;
              pDiffY = origDiffY;
      }
      else
      {
            tx = origToX;
            ty = origToY;
            trace("Last Draw!")
            trace("Drawing from: ", fromX, ",", fromY, "to:, ", tx, ",", ty);
            graphics.moveTo(fromX, fromY);
            graphics.lineTo(tx, ty)
            increment = 0;
      }
      fromX = tx;
      fromY = ty;
      increment--;
      if(increment <= 0)
        removeEventListener(Event.ENTER_FRAME, onEnterFrame); 
}

then i call it in different frames like this

drawLineProgressively(nude1.x, nude1.y, nude2.x, nude2.y,5);

the problem is when it draw lines at the same time it stops working and i cant use it to draw multiple of lines at the same time

drawLineProgressively(nude2.x, nude2.y, nude3.x, nude3.y, 20);
drawLineProgressively(nude2.x, nude2.y, nude4.x, nude4.y, 10);

im new in flash and sorry for my bad en

Aucun commentaire:

Enregistrer un commentaire