Flash 如何清除屏幕上的旧线条?
我正在把一个程序从AS2翻译成AS3。我有一个简单的动画,在屏幕上移动的球之间画线,但我不知道如何使用clear()删除旧的球。这就是我得到的: 代码如下:Flash 如何清除屏幕上的旧线条?,flash,actionscript-3,flash-builder,Flash,Actionscript 3,Flash Builder,我正在把一个程序从AS2翻译成AS3。我有一个简单的动画,在屏幕上移动的球之间画线,但我不知道如何使用clear()删除旧的球。这就是我得到的: 代码如下: function moveBalls(e:Event):void { var nodeA:MovieClip; var nodeB:MovieClip; var line:Sprite = new Sprite(); for
function moveBalls(e:Event):void
{
var nodeA:MovieClip;
var nodeB:MovieClip;
var line:Sprite = new Sprite();
for (var i:uint = 0; i < getLength(nodes); i++)
{
nodeA = nodes[i];
for (var j:uint = i + 1; j < getLength(nodes); j++)
{
nodeB = nodes[j];
var dx:Number = nodeB.x - nodeA.x;
var dy:Number = nodeB.y - nodeA.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
if (dist < minDist)
{
line.graphics.lineStyle(1, 0x00ffff);
line.graphics.moveTo(nodeA.x+nodeA.width/2, nodeA.y+nodeA.height/2);
line.graphics.lineTo(nodeB.x+nodeB.width/2, nodeB.y+nodeB.height/2);
addChild(line);
var ax:Number = dx * k * .5;
var ay:Number = dy * k * .5;
nodeA.vx += ax;
nodeA.vy += ay;
nodeB.vx += ax;
nodeB.vy += ay;
}
}
}
}
函数移动球(e:事件):无效
{
变量nodeA:MovieClip;
var节点b:MovieClip;
变量行:Sprite=新Sprite();
对于(变量i:uint=0;i
试试这个演示:
var s:Shape = new Shape();
addChild(s);
stage.addEventListener(MouseEvent.CLICK, _click);
function _click(e:MouseEvent=null):void
{
s.graphics.clear();
s.graphics.lineStyle(2, 0xFF0000);
s.graphics.lineTo(
Math.random()*stage.stageWidth,
Math.random()*stage.stageHeight
);
}
_click();
提示:
Shape
比Sprite
更轻,建议在您的场景中使用。由于line
是moveBalls
函数中的一个局部变量,因此每次调用它时都会不断添加更多的行(每次都会创建一个新形状,并在方法结束时保留而不是放弃,因为它已添加到显示列表中)
将line
设为字段变量,而不是
var line:Shape = new Shape ();
// I took @Marty Wallace's suggestion and went with Shape here
召唤
在moveBalls方法的开始部分。我得到这个是为了正确地清除行,但是当我将行移出函数时,它每次都会被覆盖,最后我只得到一行,而不是多行。感谢关于形状的提示。好的,我不确定如何,但在重新评估您的建议后,它会起作用,而且不仅仅是是的,但我不能像上面描述的那样让它中断。所以谢谢。@Sam Shudder:P Flash有时很疯狂。
line.graphics.clear();