Mouse events on drawn/hidden objects

**** UPDATE ****
After a lengthy discussion with Tink over IM (you can see in my comment below), we determined that you don’t need to set the alpha to .0051 – this was a trick that Keith used with his Apollo app to make the BG clickable, but I assumed it was true for flash and never bothered to think past that πŸ˜‰ You can set the alpha to 0 and still click on a Sprite on stage.

Now, I tried the suggestion of using a Sprite container and drawing in it, then assigning that as the hit area of a parent sprite container, but that was horribly slow compared to the blendMode method.

So, BlendMode, has shown to be the fastest of the 3 methods described here. Of course, causing the player to deal with alpha is going to cost you in CPU, so since BlendMode.ERASE essentially skips those calcs, it seems to be the fastest solution so far.

**** END UPDATE ****

Well, leave it to Carlos to come up with this little gem. I’d been at Keith Peter’s talk on “thinking outside the box” at FITC Toronto (which completely \m/), and one of the little gems I got from that (besides, bang/hole/shoot) was that the lowest alpha value you could set on a sprite and still have Mouse interactivity was .0051. Apparently, Keith sat there and entered in values until he hit a number that worked πŸ™‚

*Note:Tink has helped me dispell this myth – .0051 was used for Keith’s Apollo app for the background, but is not necessary to do in flash for this type of thing. Setting alpha to 0 will allow you to interact with your containers.

Now, I’d used this little bit o’ happiness in InteractiveSceneManager with Papervision3D and it’s worked well. However, we knew this came at a price with the render.

Carlos just hit me with this work around by using BlendMode.ERASE on the sprite container – and it’s fast πŸ™‚ He got the idea off of Tinic’s blog with a nice explanation of why it’s so much faster.

Anyway, for what I was doing, this was a rockin’ solution especially when we’re trying to preserve as much CPU for 3D as we can πŸ˜‰

[as]var mc:MovieClip = new MovieClip();
addChild(mc);,1);, 0, 10);

mc.blendMode = BlendMode.ERASE;

mc.addEventListener(MouseEvent.CLICK, handleClick);

function handleClick(e:MouseEvent):void

Hope that helps, sorry for the confusion

    • kenny bunch
    • August 7th, 2007

    Sweet, I am feeling this.

  1. “the lowest alpha value you could set on a sprite and still have Mouse interactivity was .0051. ”

    This is untrue. A DisplayObjec must be visible and have mouseEnabled set to true to pick up moouse events. The alpha property does not affect this.

  2. you’re right!

    After getting my ass kicked over IM for about 30 minutes by Tink, I finally realized what he was trying to say. The .0051 was from Keith’s discussion on Apollo and having the bg clickable for his “shoot the desktop” game.

    So, I assumed that to be true with this case, but it’s not πŸ˜‰

    Sorry for the confusion. Although, the code above does work, it’s not necessary.

    And on top of that, Tink was able to show me a way to use a Shape to define the hitArea and achieve the same result which is even faster.

    I LOVE it when Tink gets involved. LOVE IT!

  3. Yeah, thanks John. Tink got on my ass about this, and even though I completely agreed with him the whole time, it took me 15 minutes to get him to shut up. πŸ™‚


  4. yeah, even later last night, tink came on IM to hash it a bit more with me, which was good because he finally understood my situation and use-case.

    Ended up that the Sprite/Shape idea wasn’t possible since you can’t assign a shape as a hitArea (has to be a Sprite), and was eventually much slower than even the alpha trick I was doing.

    but I love it when Tink pings me after a post – you know that you’ll end up at the truth eventually πŸ˜‰

  5. I knew I should have mentioned it when I saw your post, but Tink did it, so now I’m happy. My thoughts at the time: “0.0051? WTF?! Zero has always worked for me! …well, maybe I’m just not remembering correctly…”

    Lesson learned: Don’t question myself, even when John Grden says differently. πŸ˜‰

  6. very interesting !!
    But in my case, the result is different from you.
    it was more fast the result worked around by using alpha rather than by using BlendMode.ERASE.(cpu usage)

    i just wonder if you examine my file.

    sample file link—->

    • Brian Bender
    • November 8th, 2007


    Just tried this. mc.alpha = 0 works as well.

  7. yeah that’s why I updated my post originally (read the update section). The 0.0051 thing is actually for an apollo application where you want the background to receive mouse events and yet be transparent. I heard it from Keith at one of his discussions and I just didn’t put it together that it was really for flash, but just apollo in that one fringe area.

  8. Yeah, Thanks john, you are really correct, I tried , it really works.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s