InteractivePNG

Like magic, transparent parts of a PNG in your MovieClip are ignored during mouse interactions. Check it out!

Normally the clear areas of a PNG are treated as solid, which can be especially frustrating when dealing with a lot of images that overlap each other because they tend to block mouse interactions on the clips below them.

This utility fixes that so that mouse events don't occur until you bump against a solid pixel, or a pixel of any transparency value besides totally clear. InteractivePNG lets you set an alphaTolerance level to determine what transparency level will register as a hit.

This was surprisingly tricky to write, so I'm releasing it open source in hopes that it helps someone out there.

Source and Documentation here

I chose not to use a mask, because that would mean managing the displaylist outside the movieclip, I wanted this to work for any freestanding movieclip without any complicated management within the program. I've also heard of people creating an overlay bitmap with all the parts and running hit detection on that, but that is a little clunky – it adds filesize and makes it hard to update your layout.

I know it looks extremely simple, but if you're curious, here's what goes into it. First I detect & suppress mouse interactions at which time I toggle the clip's mouseEnabled flag off, and use an ENTER_FRAME event to detect when the mouse bumps into the edge of the image and reenable the mouse, toggling it off again during roll out. It uses the native method BitmpaData.hitTest. Finally when the mouse leaves the bounds of the movieclip, the tracking is turned off and the system is reset to listen for the mouse to knock again. It was particularly tricky to keep the cursor hand from flickering when the edge of the image is passed when buttonMode is turned on, which is done by temporarily caching that property on the initial round of suppressed events. Like I said, it looks simple, but...!

28 Responses to “InteractivePNG”

  1. [...] View InteractivePNG Post [...]

  2. [...] Moses Gunesch’s InteractivePNG for the arrow image hit [...]

  3. [...] InteractivePNG is a very handy little utility class that allows you to disable the hit area of a PNG if the pixels are transparent. [...]

  4. [...] MosesSuposses blogpost with demo and sources [...]

  5. [...] mere om og download den her: InteractivePNG Tags: png Del:These icons link to social bookmarking sites where readers can share and discover [...]

  6. [...] solution lies ahead! InteractivePNG is a class that ‘ignores’ mouse interactions on transparant pixels based on the [...]

  7. [...] was to do a pixel hit test, which proved very frustrating. I consulted Matt who referred me to the InteractivePNG class, which very easily told the mouse to ignore the PNG’s alpha. The problem it didn’t [...]

  8. [...] check the demo and download the class here: InteractivePNG [...]

  9. [...] search if someone did great job on some workaround and I have found really great AS3 class called InteractivePNG. You can set even alphaTolerance to tell class if your MouseEvent event should be catched on [...]

  10. [...] Blog post [...]

  11. [...] InteractivePNG – useful as class for transparent hit states – http://blog.mosessupposes.com/?p=40 # Share and [...]

  12. [...] selbst programmieren wenns schon jemand gemacht hat? MosesSupposes Blog Archive InteractivePNG Diese Klasse macht genau das was du willst – du kannst durch die transparenten PNG-Bereiche [...]

  13. [...] This is a very cool utility class that allows you to ignore transparent pixels on rollover. Not only that, it also lets you set an alpha tolerance level so you can have complete control over how much transparency is needed to register a hit. It’s pretty cool, check it out! [...]

  14. [...] InteractivePNG is a nifty class that’s been around for a while. Instead of extending MovieClip, you can extend InteractivePNG and it will capture all the mouse events and figure out if the mouse event was actually set off by transparent (or semi-transparent) pixel in a PNG. If so, it will ignore the event and let anything behind the transparent pixel receive the mouse event instead. [...]

  15. [...] InteractivePNG [...]

  16. [...] so far or are not dynamic enough for general use. The best workaround I’ve used so far is InteractivePNG created by Moses [...]

  17. [...] so far or are not dynamic enough for general use. The best workaround I’ve used so far is InteractivePNG created by Moses [...]

  18. [...] transparent areas, the events just don’t get through. Moses over at MosesSupposes created an InteractivePNG solution that does some interesting mouse handling and hit detection.In the above example (click [...]

  19. [...] was to do a pixel hit test, which proved very frustrating. I consulted Matt who referred me to the InteractivePNG class, which very easily told the mouse to ignore the PNG’s alpha. The problem it didn’t [...]

  20. [...] 这个问题被提到过很多次了,这里是一种解决方案,http://l4cd.net/blog/post-ignore-the-events-of-png-transparency.html,是通过sprite的hitArea来解决的。可是经过测试Sprite的hitArea效率很低,而且还需要创建一个area对象,以提供响应鼠标的区域,增加了开销。google了一下,发现InteractivePNG在这方面做得很好。于是就模仿着写了一个更轻量级的类,这个类更方面于扩展。下载在这里。 [...]

  21. [...] [...]

  22. [...] [...]

  23. Begnor BlackBerry Blog…

    [...]MosesSupposes » Blog Archive » InteractivePNG[...]…

  24. [...] [...]

  25. [...] google了一下,发现InteracitvePNG在这方面做得很好。 [...]

Leave a Reply

You must be logged in to post a comment.