多次调用函数而不会在actionscript中发生冲突

时间:2015-06-26 15:37:56

标签: actionscript-3 flash line animated

我在舞台上有一些点,想要用线连接它们。我使用此功能来动画线条的绘制。我找到了它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); 
}

然后我在不同的帧中调用它

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

问题是当它在停止工作的同时画线时 我不能用它同时绘制多行

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

编辑: 我创建了一个类

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


    }

} 

但只有最后一行有效

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

我是flash的新手,对不起我的坏人

2 个答案:

答案 0 :(得分:0)

您必须将该代码封装到类中,并且您可以同时创建和操作任意数量的代码。

答案 1 :(得分:0)

所以基本相同&#34; onEnterFrame&#34;调用所有行(但每帧多次)。您的所有成员值(dx,dy,tox ...)仅在您上次调用该函数时保存。

以下是修复代码以使其正常工作的最简单方法:

而不是写作:

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

请改为:

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

但是,这是一个糟糕的解决方案,而且你要解决这个问题。但是,它会使你的东西工作:[]