线路从一个实例到另一个工作不动作脚本3.0 [英] Line from one instance to another not working Action Script 3.0
问题描述
我想创建到可移动的情况下,这是可拖动,并有一条线连接它们自动更新。
I am trying to create to movable instances, that are draggable, and which have a line connecting them which updates automatically.
的事情是:原线只是去随机并作为被拖动的情况下(MC1,MC2)不更新。 这里是code我有这么远:
The thing is: The original line just goes randomly and doesn't update as the instances (mc1, mc2) are being dragged. Here is the code I have got so far:
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);
谁能告诉我去哪里我听错了?
Can anyone tell me where have I got it wrong?
非常感谢任何帮助!
推荐答案
您需要做的是捕获用户的鼠标为他(她)是拖动影片剪辑的运动。
What you need to do is capture the movement of the users mouse as (s)he is dragging the MovieClip.
首先创建一个isDragging变量 VAR isDragging:布尔=假;
,并设置为true,当鼠标向下假当鼠标向上
First create a isDragging variable var isDragging:Boolean = false;
and set this to true when mouse down and false when mouse up.
接着一个的MouseEvent.MOUSE_MOVE事件侦听器添加到时间线上行
Then add a MouseEvent.MOUSE_MOVE event listener to the line timeline
addEventListener(MouseEvent.MOUSE_MOVE, function (e:MouseEvent):void
{
if (isDragging) {
drawConnectiveLine(mc1, mc2);
}
return;
});
方法 drawConnectiveLine
将更新的行矢量形状移到影片剪辑的{X,Y}。
The method drawConnectiveLine
will update the line vector shape to move to {x, y} of the movieclips.
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);
}
修改您的pre-现有的脚本:
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);
}
});
这篇关于线路从一个实例到另一个工作不动作脚本3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!