如何在as3中像在flash中一样进行捕捉?
舞台上放置了很多电影片段?现在我想使用拖放到影片剪辑内来对齐它们。我希望将选定的影片剪辑放在该影片剪辑下方时出现垂直对齐线,如所附图像所示。
这是一个图像:
这是代码:
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);
}
}
}
但此代码无法正常工作。
请帮忙!
提前致谢。
There are many movieclips placed on stage? Now I want to align them using drag and drop inside movieclips.I want the vertical alignment line appear while placing the selected movieclip under that movieclip as shown in the attached image.
Here is an image:
Here is the code:
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);
}
}
}
But this code isnt working properly.
Please Help!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我想要的答案,我犯了一个错误,我在匹配条件内设置了数组索引,该索引应该在 MOUSE_UP 事件的 for 循环内设置。
Here is the answer what all i want,I was doing a mistake that i was setting the array index inside matching condition ,which should be set inside for loop on MOUSE_UP event.