从一个实例到另一个实例的行不能正常工作Action Script 3.0

时间:2011-11-02 18:17:00

标签: flash actionscript-3 graphics lines instances

我正在尝试创建可拖动的可移动实例,并且有一条连接它们的线路会自动更新。

事情是:原始行只是随机进行,并且不会随着实例(mc1,mc2)被拖动而更新。  这是我到目前为止的代码:

 mc1.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
{
    e.currentTarget.startDrag();
});

mc1.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
{
    e.currentTarget.stopDrag();
});
mc2.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
{
    e.currentTarget.startDrag();
});

mc2.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
{
    e.currentTarget.stopDrag();
});

var mc:MovieClip = new MovieClip();
mc.graphics.beginFill(0x000000);
mc.graphics.lineStyle(2,0x000000);

//start drawing the line
mc.graphics.moveTo(mc1.x,mc1.y);
mc.graphics.lineTo(mc2.x,mc2.y);
mc.graphics.endFill();

//Position your new movie clip
addChild(mc);

谁能告诉我哪里弄错了?

非常感谢你帮助!!!!

2 个答案:

答案 0 :(得分:0)

下面的阻止可能对您有帮助。

mc.graphics.clear();
//mc.graphics.beginFill(0x000000); //no need fills
mc.graphics.lineStyle(2,0);

//start drawing the line
mc.graphics.moveTo(mc1.x,mc1.y);
mc.graphics.lineTo(mc2.x,mc2.y);
//mc.graphics.endFill(); //no need to end it.

答案 1 :(得分:0)

您需要做的是捕捉用户鼠标的移动,因为他正在拖动MovieClip。

首先创建一个isDragging变量var isDragging:Boolean = false;并在鼠标按下时将其设置为true,在鼠标按下时将其设置为false。

然后将MouseEvent.MOUSE_MOVE事件侦听器添加到行时间轴

addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void
{
    if (isDragging) {
        drawConnectiveLine(mc1, mc2);
    }
    return;
});

方法drawConnectiveLine将更新线条矢量形状以移动到动画片段的{x,y}。

function drawConnectiveLine(d:DisplayObject, d2:DisplayObject):void {
    mc.graphics.clear();
    mc.graphics.lineStyle(2,0);
    mc.graphics.moveTo(d.x,d.y);
    mc.graphics.lineTo(d2.x,d2.y);
}

对预先存在的脚本的修改:

var isDragging:Boolean = false;
var mc:MovieClip = new MovieClip();
mc.graphics.lineStyle(2,0);
mc.graphics.moveTo(mc1.x,mc1.y);
mc.graphics.lineTo(mc2.x,mc2.y);
addChild(mc);

mc1.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
{
    isDragging = true;
    e.currentTarget.startDrag();
});

mc1.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
{
    isDragging = false;
    e.currentTarget.stopDrag();
});

mc2.addEventListener(MouseEvent.MOUSE_DOWN, function (e:MouseEvent):void
{
    isDragging = true;
    e.currentTarget.startDrag();
});
mc2.addEventListener(MouseEvent.MOUSE_UP, function (e:MouseEvent):void
{
    isDragging = false;
    e.currentTarget.stopDrag();
});
addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void
{
    if (isDragging) {
        mc.graphics.clear();
        mc.graphics.lineStyle(2,0);
        mc.graphics.moveTo(mc1.x,mc1.y);
        mc.graphics.lineTo(mc2.x,mc2.y);
    }
});