call a function multiple times without conflict in actionscript

158 views Asked by At

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);

Edit: i created a class

package 
{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.display.*;
    public class aline extends MovieClip   

    {
        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;

        public function aline()
        {
            trace("draw line");
        }


        public 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(2, 0Xff0000);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        public 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);
            }
        }


    }

} 

but only the last line works

var s:aline =new aline();
s.drawLineProgressively(nude1.x,nude1.y,nude2.x,nude2.y,5);
s.drawLineProgressively(nude2.x,nude2.y,nude3.x,nude2.y,3);
s.drawLineProgressively(nude3.x, nude3.y, nude4.x, nude4.y, 5);
s.drawLineProgressively(nude2.x, nude2.y, nude4.x, nude4.y, 5);
addChild(s);

im new in flash and sorry for my bad en

2

There are 2 answers

1
Azzy Elvul On BEST ANSWER

You must capsulate that code into class and than you can create and manipulate as many as you want lines simultaniously.

0
Daniel MesSer On

So basically the same "onEnterFrame" is called for all the lines (but multiple times each frame). All your member values (dx, dy, tox ...) are only saved for the last time you called that function.

Here's the easiest way to fix your code to make it work:

Instead of writing:

var s:aline =new aline();
s.drawLineProgressively(nude1.x,nude1.y,nude2.x,nude2.y,5);
s.drawLineProgressively(nude2.x,nude2.y,nude3.x,nude2.y,3);
s.drawLineProgressively(nude3.x, nude3.y, nude4.x, nude4.y, 5);
s.drawLineProgressively(nude2.x, nude2.y, nude4.x, nude4.y, 5);
addChild(s);

Do this instead:

var s:aline;
s = new aline();
s.drawLineProgressively(nude1.x ...);
addChild(s);

s = new aline();
s.drawLineProgressively(nude2.x ...);
addChild(s);

s = new aline();
s.drawLineProgressively(nude3.x ...);
addChild(s);

s = new aline();
s.drawLineProgressively(nude4.x ...);
addChild(s);

However, this is a bad solution and you're going down a bad route for solving this issue. But, it will make your stuff work :[]