ActionScript 拖放显示带有蒙版和过滤器的对象?

发布于 2024-09-05 06:15:44 字数 2551 浏览 5 评论 0原文

我创建了一个精灵来在舞台上拖放。精灵被遮蔽并具有它的遮罩作为它的子元素,这样它也会与精灵一起拖动。一切正常,直到我向精灵添加阴影滤镜。当添加阴影时,如果鼠标事件发生在精灵被添加到舞台上的原始位置内,我只能使用 mousedown 来拖动精灵,使用 mouseup 来放置精灵。

我该如何解决这个问题?这可能是 10.1 的问题吗?如果不是我做错了什么?

var thumbMask:Sprite = new Sprite();
thumbMask.graphics.beginFill(0, 1);
thumbMask.graphics.drawRoundRect(0, 0, 100, 75, 25, 25);
thumbMask.graphics.endFill();

var thumb:Sprite = new Sprite();
thumb.graphics.beginFill(0x0000FF, 1);
thumb.graphics.drawRect(0, 0, 100, 75);
thumb.graphics.endFill();

thumb.addEventListener(MouseEvent.MOUSE_DOWN, drag);
thumb.addEventListener(MouseEvent.MOUSE_UP, drop);

thumb.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

thumb.addChild(thumbMask);
thumb.mask = thumbMask;
addChild(thumb)

function drag(evt:MouseEvent):void
    {
    evt.target.startDrag();
    trace("drag");
    }

function drop(evt:MouseEvent):void
    {
    evt.target.stopDrag();
    trace("drop");
    }

<强>---------------- 更新的解决方案 ----------------

感谢二进制文件的建议,通过使用cacheAsBitmap解决了该问题。然而,当我将cacheAsBitmap应用到容器时,该容器容纳了已经应用了阴影的蒙版拇指精灵,鼠标事件没有按预期工作。阅读文档后,我了解到向精灵添加过滤器会自动激活该精灵的cacheAsBitmap属性:

cacheAsBitmap 属性是 每当您自动设置为 true 将过滤器应用于显示对象 (当它的过滤器数组不为空时), 如果显示对象有过滤器 应用于它,cacheAsBitmap 是 该显示报告为 true 对象,即使您设置了属性 为假。如果您清除所有过滤器 显示对象,cacheAsBitmap 设置更改为上次设置 设置为。 1

所以我相信这个错误是由于同一容器精灵中有 2 个 cacheAsBitmap 属性引起的。因此,在这种情况下,我只是将过滤器添加到容器中,而不是添加到蒙版拇指精灵中。

var thumbMask:Sprite = new Sprite();
thumbMask.graphics.beginFill(0, 1);
thumbMask.graphics.drawRoundRect(0, 0, 100, 75, 25, 25);
thumbMask.graphics.endFill();

var thumb:Sprite = new Sprite();
thumb.graphics.beginFill(0x0000FF, 1);
thumb.graphics.drawRect(0, 0, 100, 75);
thumb.graphics.endFill();

thumb.addChild(thumbMask);
thumb.mask = thumbMask;

var container: Sprite = new Sprite();
container.addChild(thumb);
container.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

//if there is no filters applied or if the filters array is empty, use:
//container.cacheAsBitmap = true;

addChild(container);

container.addEventListener(MouseEvent.MOUSE_DOWN, drag);
container.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(evt:MouseEvent):void
    {
    evt.target.startDrag();
    trace("drag");
    }

function drop(evt:MouseEvent):void
    {
    evt.target.stopDrag();
    trace("--DROP");
    }

i've created a sprite to drag and drop around the stage. the sprite is masked and has it's mask as it's child so that it too will drag along with the sprite. everything works fine until i add a drop shadow filter to the sprite. when the drop shadow is added, i can only mousedown to drag and mouseup to drop the sprite if the mouse events occur within the original location of the sprite when it was added to the stage.

how can i fix this problem? could this be an issue with 10.1? if not what am i doing wrong?

var thumbMask:Sprite = new Sprite();
thumbMask.graphics.beginFill(0, 1);
thumbMask.graphics.drawRoundRect(0, 0, 100, 75, 25, 25);
thumbMask.graphics.endFill();

var thumb:Sprite = new Sprite();
thumb.graphics.beginFill(0x0000FF, 1);
thumb.graphics.drawRect(0, 0, 100, 75);
thumb.graphics.endFill();

thumb.addEventListener(MouseEvent.MOUSE_DOWN, drag);
thumb.addEventListener(MouseEvent.MOUSE_UP, drop);

thumb.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

thumb.addChild(thumbMask);
thumb.mask = thumbMask;
addChild(thumb)

function drag(evt:MouseEvent):void
    {
    evt.target.startDrag();
    trace("drag");
    }

function drop(evt:MouseEvent):void
    {
    evt.target.stopDrag();
    trace("drop");
    }

----------------
UPDATED SOLUTION
----------------

thanks to the binary's suggestion, the issue is solved by using cacheAsBitmap. however, when i applied cacheAsBitmap to the container, which was housing the masked thumb sprite that has already a drop shadow applied, the mouse events were not working as well as expected. after reading the docs, i learned that adding a filter to a sprite automatically activates the cacheAsBitmap property for that sprite:

The cacheAsBitmap property is
automatically set to true whenever you
apply a filter to a display object
(when its filter array is not empty),
and if a display object has a filter
applied to it, cacheAsBitmap is
reported as true for that display
object, even if you set the property
to false. If you clear all filters for
a display object, the cacheAsBitmap
setting changes to what it was last
set to. 1

so i believe the error was caused by having 2 cacheAsBitmap properties within the same container sprite. therefore, in this situation, i've simply added the filter to the container rather than to the masked thumb sprite.

var thumbMask:Sprite = new Sprite();
thumbMask.graphics.beginFill(0, 1);
thumbMask.graphics.drawRoundRect(0, 0, 100, 75, 25, 25);
thumbMask.graphics.endFill();

var thumb:Sprite = new Sprite();
thumb.graphics.beginFill(0x0000FF, 1);
thumb.graphics.drawRect(0, 0, 100, 75);
thumb.graphics.endFill();

thumb.addChild(thumbMask);
thumb.mask = thumbMask;

var container: Sprite = new Sprite();
container.addChild(thumb);
container.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

//if there is no filters applied or if the filters array is empty, use:
//container.cacheAsBitmap = true;

addChild(container);

container.addEventListener(MouseEvent.MOUSE_DOWN, drag);
container.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(evt:MouseEvent):void
    {
    evt.target.startDrag();
    trace("drag");
    }

function drop(evt:MouseEvent):void
    {
    evt.target.stopDrag();
    trace("--DROP");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-09-12 06:15:44

您也可以通过将拇指夹包装到容器中来解决此问题。
cacheAsBitmap 设置为 true 并调整事件处理。

    thumb.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

//  thumb.addEventListener(MouseEvent.MOUSE_DOWN, drag);
//  thumb.addEventListener(MouseEvent.MOUSE_UP, drop);

var cont: Sprite = new Sprite();
    cont.addChild(thumb);
    cont.cacheAsBitmap = true;
    cont.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    cont.addEventListener(MouseEvent.MOUSE_UP, drop);

    addChild(cont);

所以也许你不需要为你的阴影处理第二个精灵。
希望这有帮助..
问候..

u can work around this too by wrapping your thumb clip into a container.
set cacheAsBitmap to true and adjust the event-handling.

    thumb.filters = [new DropShadowFilter(0, 0, 0, 1, 20, 20, 1.0, 3)];

//  thumb.addEventListener(MouseEvent.MOUSE_DOWN, drag);
//  thumb.addEventListener(MouseEvent.MOUSE_UP, drop);

var cont: Sprite = new Sprite();
    cont.addChild(thumb);
    cont.cacheAsBitmap = true;
    cont.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    cont.addEventListener(MouseEvent.MOUSE_UP, drop);

    addChild(cont);

so maybe you'll don't have to deal with a second sprite for your dropshadow.
hope this helps..
regards..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文