多次调用一个函数而不会在动作脚本中发生冲突
call a function multiple times without conflict in actionscript
我在舞台上有一些点,想用线将它们连接起来。我使用此功能来动画绘制线条。我找到了 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);
编辑:
我创建了一个 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);
}
}
}
}
但只有最后一行有效
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 的新手,很抱歉我的错误
您必须将该代码封装到 class 中,然后您可以同时创建和操作任意多行。
所以基本上相同的 "onEnterFrame" 被调用所有行(但每帧多次)。您的所有成员值(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);
但是,这是一个糟糕的解决方案,而且您正在走一条错误的道路来解决这个问题。但是,它会让你的东西工作:[]
我在舞台上有一些点,想用线将它们连接起来。我使用此功能来动画绘制线条。我找到了 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);
编辑: 我创建了一个 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);
}
}
}
}
但只有最后一行有效
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 的新手,很抱歉我的错误
您必须将该代码封装到 class 中,然后您可以同时创建和操作任意多行。
所以基本上相同的 "onEnterFrame" 被调用所有行(但每帧多次)。您的所有成员值(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);
但是,这是一个糟糕的解决方案,而且您正在走一条错误的道路来解决这个问题。但是,它会让你的东西工作:[]