舞台上有很多电影剪辑吗?现在我想使用拖放在movieclips中来对齐它们。我想在将选定的动画片段放在该动画片段下时显示垂直对齐线,如附图所示。
这是一张图片:
以下是代码:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
private var obj:Rectangle1 = new Rectangle1();
private var obj1:Rectangle1 = new Rectangle1();
private var obj2:Rectangle1 = new Rectangle1();
private var obj3:Rectangle1=new Rectangle1();
public var Obj_Arr:Array = [];
private var p:VerticalLine;
public function Main()
{
obj.x=20;
obj.y=100;
addChild(obj);
obj1.buttonMode = true;
trace("init()")
obj.buttonMode=true;
obj1.x=150;
obj1.y=100;
addChild(obj1);
obj2.buttonMode=true;
obj2.x=280;
obj2.y=100;
addChild(obj2);
obj3.buttonMode=true;
obj3.x=430;
obj3.y=100;
addChild(obj3);
p = new VerticalLine();
addChild(p);
p.visible = false;
Obj_Arr.push(obj);
Obj_Arr.push(obj1);
Obj_Arr.push(obj2);
Obj_Arr.push(obj3);
for (var i:int = 0; i < Obj_Arr.length; i++)
{
Obj_Arr[i].index = i;
Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
}
}
private function OnDown(e:MouseEvent):void
{
e.target.startDrag();
trace(e.target.index);
trace("remove...")
Obj_Arr.splice(e.target.index,1)
e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
}
private function OnMove(e:MouseEvent):void
{
for (var i:int = 0; i < Obj_Arr.length; i++)
{
if(e.target.x==(Obj_Arr[i].x))
{
p.visible = true;
p.x = Obj_Arr[i].x;
p.y = Obj_Arr[i].y;
i = Obj_Arr.length;
}
else
{
p.visible =false;
}
}
}
private function OnUp(e:MouseEvent):void
{
p.visible =false;
e.target.stopDrag();
Obj_Arr.push(e.target);
for (var i:int = 0; i < Obj_Arr.length; i++)
{
if(e.target.index == (Obj_Arr[i].index))
{
trace("add...");
Obj_Arr[i].x = e.target.x;
Obj_Arr[i].index = i;
}
}
e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
}
}
}
但是这段代码无法正常工作。
请帮助!
提前致谢。
答案 0 :(得分:2)
以下是我想要的答案,我正在做一个错误,我在匹配条件中设置数组索引,应该在MOUSE_UP事件中设置for循环。
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip
{
private var obj:Rectangle1 = new Rectangle1();
private var obj1:Rectangle1 = new Rectangle1();
private var obj2:Rectangle1 = new Rectangle1();
private var obj3:Rectangle1=new Rectangle1();
public var Obj_Arr:Array = [];
private var vertical_line:VerticalLine;
private var horizontal_line:HorizontalClass;
public function Main()
{
obj.x=20;
obj.y=100;
addChild(obj);
obj1.buttonMode = true;
obj.buttonMode=true;
obj1.x=150;
obj1.y=100;
addChild(obj1);
obj2.buttonMode=true;
obj2.x=280;
obj2.y=100;
addChild(obj2);
obj3.buttonMode=true;
obj3.x=430;
obj3.y=100;
addChild(obj3);
vertical_line = new VerticalLine();
addChild(vertical_line);
vertical_line.visible = false;
horizontal_line = new HorizontalClass();
addChild(horizontal_line);
horizontal_line.visible = false;
Obj_Arr.push(obj);
Obj_Arr.push(obj1);
Obj_Arr.push(obj2);
Obj_Arr.push(obj3);
for (var i:int = 0; i < Obj_Arr.length; i++)
{
Obj_Arr[i].index = i;
Obj_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, OnDown);
Obj_Arr[i].addEventListener(MouseEvent.MOUSE_UP, OnUp);
}
}
private function OnDown(e:MouseEvent):void
{
e.target.startDrag();
Obj_Arr.splice(e.target.index,1)
e.target.addEventListener(MouseEvent.MOUSE_MOVE, OnMove);
}
private function OnMove(e:MouseEvent):void
{
for (var i:int = 0; i < Obj_Arr.length; i++)
{
if(e.target.x==(Obj_Arr[i].x))
{
trace("vertical...");
vertical_line.visible = true;
vertical_line.x = Obj_Arr[i].x;
vertical_line.y = Obj_Arr[i].y;
i = Obj_Arr.length;
}
else
{
vertical_line.visible =false;
}
}
for (var i:int = 0; i < Obj_Arr.length; i++)
{
if(e.target.y==(Obj_Arr[i].y))
{
trace("horizontal...");
horizontal_line.visible = true;
horizontal_line.x = Obj_Arr[i].x;
horizontal_line.y = Obj_Arr[i].y;
i = Obj_Arr.length;
}
else
{
horizontal_line.visible =false;
}
}
}
private function OnUp(e:MouseEvent):void
{
vertical_line.visible = false;
horizontal_line.visible = false;
e.target.stopDrag();
Obj_Arr.push(e.target);
for (var i:int = 0; i < Obj_Arr.length; i++)
{
if(e.target.index == (Obj_Arr[i].index))
{
Obj_Arr[i].x = e.target.x;
}
Obj_Arr[i].index = i;
}
e.target.removeEventListener(MouseEvent.MOUSE_MOVE, OnMove);
}
}
}