Flash AS3 - 子 MovieClip 上的 StartDrag() 内的 StartDrag()
嘿大家, 我的代码列在下面。
正如你所看到的,我有一个容器 MC,我已将其添加到舞台上。我使用 Rectangle() 设置其拖动约束。然后,我将“猫”子动画剪辑添加到容器中,并且我希望它也可以拖动。然而,当我在测试MC时点击我的猫。它射向舞台上的 x=0 y=0 点并且不动。
容器MC可以毫无困难地移动。
如果我从容器 startdrag() 函数中删除矩形边界。两个 MC 都可以毫无问题地拖动。
任何帮助都会很棒。
谢谢
//panning ability
my_x = 800 - myImage.width;
my_y = 480 - myImage.height;
myWidth = 0 - my_x;
myHeight = 0 - my_y;
container.addEventListener(MouseEvent.MOUSE_DOWN, bgMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, bgMouseUp);
function bgMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}
function bgMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
//adding ze cat
cat = new ACat();
container.addChild(cat);
cat.x = 100;
cat.y = 400;
cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown);
cat.addEventListener(MouseEvent.MOUSE_UP, catMouseUp);
function catMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false);
}
function catMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
hey everyone,
my code is listed below.
as you can see, I have a container MC which I have added to the stage. I set its drag constraints using a Rectangle(). I then add the 'cat' child movieclip to the container, and I want this to be dragable too. However, as soon as I click on my cat when testing the MC. It shoots to point x=0 y=0 on the stage and doesn't move.
The container MC can be moved without any trouble.
If I remove the rectangle bounds from the containers startdrag() function. both MC's can be dragged without any issue.
any help would be awesome.
thanks
//panning ability
my_x = 800 - myImage.width;
my_y = 480 - myImage.height;
myWidth = 0 - my_x;
myHeight = 0 - my_y;
container.addEventListener(MouseEvent.MOUSE_DOWN, bgMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, bgMouseUp);
function bgMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false, new Rectangle(my_x, my_y, myWidth ,myHeight));
}
function bgMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
//adding ze cat
cat = new ACat();
container.addChild(cat);
cat.x = 100;
cat.y = 400;
cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown);
cat.addEventListener(MouseEvent.MOUSE_UP, catMouseUp);
function catMouseDown(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.startDrag(false);
}
function catMouseUp(evt:MouseEvent):void
{
var object = evt.currentTarget;
object.stopDrag();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 catMouseDown 函数的第一行尝试
evt.stopPropagation( )
。还可以尝试cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);
Try
evt.stopPropagation( )
on the first line of the catMouseDown function.Also try
cat.addEventListener(MouseEvent.MOUSE_DOWN, catMouseDown, true);
我认为你必须测试 currentTarget
因为当您拖动 Cat mc 时会为容器触发该事件,
请尝试类似的操作:
I think you have to test wich is the currentTarget
because the event is fired for the container when you drag the Cat mc
try something like that :
从容器中删除侦听器,并为可拖动对象创建一个基类。
这样做的优点太多了,无法一一列举。
Remove your listeners from the container, and make a base class for your dragable objects.
There are too many advantages to this to list.