Making a Movieclip which is set as mask clickable and respond to MouseEvents

别等时光非礼了梦想. 提交于 2019-12-25 01:36:59

问题


I am trying to do panning of a movieclip

I have two movieclips on stage. canvasPanel_mc and mask_mc. The former is the maskee for the mask (mask_mc). Inside mask_mc there is a movieclip dragCanvas_mc. The alpha of dragCanvas_mc is set to zero. This is code that I am using:

mask_mc.dragCanvas_mc.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
mask_mc.dragCanvas_mc.addEventListener(MouseEvent.MOUSE_OUT,onStopDrag);
mask_mc.dragCanvas_mc.addEventListener(MouseEvent.MOUSE_UP,onStopDrag);

function onStartDrag(evt:MouseEvent)
{
 canvasPanel_mc.startDrag();
}
function onStopDrag(evt:MouseEvent)
{
 canvasPanel_mc.stopDrag();
}

I have figured that since the mask_mc is set as a mask the MouseEvents are not being registered. Is there any way to make it respond to MouseEvents. Or should I work out some other way.


回答1:


the structure of your layers should be looking like this:

  • holder_mc
    • dragCanvas_mc
    • mask_mc
    • canvasPanel_mc

then:

canvasPanel_mc.mask = mask_mc;

when you are setting mask_mc as mask for canvasPanel_mc, then mask_mc becomes just a diplayobject, so that it would bypass the mouseEvents and etc...

then your code would be looking like this, it would drag all of the holder_mc:

dragCanvas_mc.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);

function onStartDrag(evt:MouseEvent)
{
    stage.addEventListener ( MouseEvent.MOUSE_UP,onStopDrag)
    startDrag();
}
function onStopDrag(evt:MouseEvent)
{
    stage.removeEventListener( MouseEvent.MOUSE_UP,onStopDrag)
    stopDrag();
}

@pkyeck solution is valid too, but only when you have no user iteraction inside the canvasPanel_mc




回答2:


the content you want to mask doesn't have to be inside the mask-movieclip. normally the mask is just a rectangular sprite/shape with nothing in it except a drawing on the graphics canvas.

var mask:Shape = new Shape();
mask.graphics.beginFill(0xff0000, 1);
mask.graphics.drawRect(0, 0, 20, 20);
addChild(mask);

then you would create the container:

var container:Sprite = new Sprite();
addChild(container);
container.mask = mask;

and then just add the eventlistener to the container:

container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
private function onMouseDown(evt:MouseEvent):void
{
  container.startDrag();
}

you could also put additional MCs into the container sprite ...

example on wonderfl: http://wonderfl.net/c/nqpN



来源:https://stackoverflow.com/questions/6832117/making-a-movieclip-which-is-set-as-mask-clickable-and-respond-to-mouseevents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!