Solved

mouseEnabled problem

Posted on 2009-04-02
16
2,242 Views
Last Modified: 2013-11-11
I've got a sprite with two child sprites. One child sprite has event listeners assigned to it for mouseover/out. the other child sprite is a bitmap which is larger than the sprite with the event listeners. Actually the face that it's a bitmap is irrelevant. Anyway, it's essentially like so:

Parent Sprite "BobsYourUncle"
      \--- Child sprite "mouseTarget" (w/event listeners)
      \--- Child sprite "evilMouseBlocker" (w/ bitmap object)

Mousing over BobsYourUncle's mouseTarget produces the correct response. However, if another instance of "BobsYourUncle" is placed behind it so that the 1st instance's evilMouseBlocker covers the 2nd instance's mouseTarget, it will not capture the mouseEvent.

I've set the mouseEnabled property of evilMouseBlocker to false, but it does no good. I'm assuming since the second instance is not a parent/child of the 1st instance.

I've set up a quick example of what I'm talking about. See the attached swf.

The nesting of the clips can't change. They mouseTarget and evilMouseBlocker both need to be in a parent container. So how do I disable the mouse for evilMouseBlocker and allow the mouseEvents to be captured in objects outside of the scope of the parent?

Hope that makes sense. Thanks in advance.
example.swf.txt
0
Comment
Question by:alien109
  • 6
  • 5
  • 4
16 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 24057123
ur fla will be more useful than the swf.
0
 
LVL 12

Author Comment

by:alien109
ID: 24057192
Sure, although this is just an example of the situation.

Thanks man.
example.fla.txt
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 24057896
ok got the file - now explain to me in terms of what's suppose to happen and what's not happening?
i.e. with instance 1 and instance 2.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 24059659
Have you also set the mouseChildren = false? make sure you do that
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 24059681
Here's a link if you want more explanation:
http://www.kirupa.com/forum/showthread.php?p=1948039

he mouseChildren property (flash.display.DisplayObjectContainer.mouseChildren) is a new property that allows you to essentially control the mouseEnabled of all the children of a sprite (or other display object container). This allows the parent sprite to have its own events without having to worry about interference with events coming from its children.
ActionScript Code:

spriteInstance.mouseChildren = false;



Though ActionScript 3 now has a SimpleButton class (flash.display.SimpleButton). this property would play an important role in making custom buttons with the MovieClip or Sprite classes. Consider what would happen if you created a rollover event that caused a sprite or movie clip-based button to remove its current graphics and create new ones for a rollover state. With the children of that sprite capable of receiving mouse events, events would fire for the children within the button - events which would propagate to the main sprite. This could mean duplicate events as well as making the event target for those events the children of the button instead of the button itself. Setting mouseChildren to false you can prevent that from happening.

The easiest demonstration is a simple click of a sprite containing another sprite. Change the value of mouseChildren between true and false and notice which sprite is designated as the target of the event:
ActionScript Code:

// main button
var spriteButton:Sprite = new Sprite();
spriteButton.name = "spriteButton";
spriteButton.mouseChildren = true;

// graphics
var spriteGraphics:Sprite = new Sprite();
spriteGraphics.name = "spriteGraphics";
spriteGraphics.graphics.beginFill(0x4080A0);
spriteGraphics.graphics.drawCircle(50, 50, 25);

// add to display list
spriteButton.addChild(spriteGraphics);
addChild(spriteButton);

// events
spriteButton.addEventListener(MouseEvent.CLICK, click);
function click(evt:MouseEvent):void {
    trace(evt.target.name);
}


When true (default), the sprite within the button that was clicked is marked as the target. When false, the button sprite itself is the target - something which is more desirable in this case.

mouseChildren = true; output
Code:

spriteButton

mouseChildren = false; output
Code:

spriteGraphics

0
 
LVL 12

Author Comment

by:alien109
ID: 24059708
The desired result would be that instance1's mouseBlocker would not prevent instance2's mouseTarget from capturing the roll_over event. Essentially, mouseBlocker doesn't block anything. Maybe a bad name for my example...

If I set mouseEnabled to false for the mouseBlocker, it works, but only for its siblings. I need it to work for anything, regardless of relationship.
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
ID: 24059973
Is mouseBlocker a MC with embedded MCs? If that's the case, mouseChildren=false is required. If not, then there's a structural problem that mouseChildren alone cannot solve. Look at your event bubbling and see if you can capture your rollovers another way - look up "hitTestPoint()" where you would pass the mouse x and y coords as a Point to the function. This would go inside a mouseMoved event handler on the Stage.

T
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 12

Author Comment

by:alien109
ID: 24062801
I can't set mouseChildren to false on the parent sprite. that will prevent the child sprite that is handling the events to not work.

This might be a no-can-do sorta thing and need to restructure things...
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 24063360
It's starting to smell that way....
0
 
LVL 12

Author Comment

by:alien109
ID: 24066120
k. i was being dumb...

if I set mouseEnabled to false on the parent and the mouseBlocker and mouseEnabled to true on the mouseTarget, it works how I was hoping.

Thanks for the help nonetheless.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 24076508
tomaugerdotcom offered the assistance to resolving the problem no?
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 24084226
i'm objecting on tomaugerdotcom's behalf, but if he has no objections then I guess you can go ahead and do what you feel is right.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 24091401
Thanks BG.  Much appreciated!

I"m not desperate for points, but I do feel that I provided the OP with a bunch of relevant information which I'm presuming helped arrive at the resolution of the issue. I'll leave it up to the OP to decide what is fair here.
0
 
LVL 12

Author Comment

by:alien109
ID: 24092977
Sorry for the delay - out of town on business...

it was sorta self deduced, but i have no problem with awarding the points. Sorry about that. And thank you both for the help and replies.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 24096669
Mucho appreciado!

T
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now