MoveClip.Visible and MouseEvent - ActionScript3 - actionscript-3

Say I have the following;
public function onBellyPatch_Two(e:MouseEvent):void
{
inBelly_Two.visible = true;
}
inBelly_Two is a MovieClip
I have two movieclips over top of each other, when you click one MovieClip another one shows up on top, and when you click that (second MovieClip)a textBox is updated.
I noticed that even if a movieclip object's visible property is false, when you click in the area where the movie clip is the MouseEvent.CLICK event is called. Is there a way to get around this? I would like to stack movieClip.
I guess one way to get around this problem would be:
removing the eventListener when movieClip is not visible and enabling the eventListener when moviclip is visible.
Is there some otherway?
Much Thanks,
Mike

Try adding:
inBelly_Two.buttonMode = false;
This will let onBellyPatch_Two be called no matter if inBelly_Two is visible or not.

Instead of removing the listener, you can just say
mc.mouseEnabled = false;
mouseEnabled docs

Related

as3 Buttonmode not working

I'm not even sure if this is a problem that can be solved through code. I have simple movie clips in an array that I'm trying to add click Event Listeners to, and I can change the buttonMode to true and add the event Listener, but only one of the movieclips actually shows the behavioral changes from the buttonMode and event Listener.
for(var d:int = 0; d < doors.length; d++)
{
doors[d].buttonMode = true;
doors[d].addEventListener(MouseEvent.CLICK, doorClick);
trace(doors[d].buttonMode);
trace(doors[d].hasEventListener(MouseEvent.CLICK));
}
all the traces return true, and I traced d and doors[d] to make sure that the problem wasn't with the array, but it isn't and only the door at index 1 works as intended. How can I find why the listeners aren't working?
Isolate your doors and loop. The code will work on its own without other elements. I've had this same problem before and it's usually because the invisible portion of another movieclip is overlaying the button objects.
If you're adding other MovieClips dynamically that may overlay the doors, locate the container movieclip of those objects and set its properties mouseEnabled and mouseChildren to false. The container and its child objects will no longer receive mouse clicks instead of your door movieclips.
The answer here clarified the issue for me:
https://stackoverflow.com/a/2686510/629407

Disable mouse event for a specific child of a movieclip

I've a MovieClip that contains lot of children.
One of them is a big (and useless) shadow the graphic designer put there to make my life harder (and also the user's one, probably)
:)
Now I'm facing a little issue: the shadow is catching the MouseEvents attached to the main MovieClip (its parent), and this is very bad because it's very distant from the rest of the graphic. I'm now trying to avoid it.
Obviously I've already tried to set the shadow mc's properties mouseEnabled and mouseChildren to false, but it doesn't work.
I've found a previous thread (here), facing the same situation. But the solution accepted looks like it's not working for me.
What I am missing?
If mouseEnabled and mouseChildren is not working for you then use e.target.name property.
But first you will have to give a name to that shadow MovieClip (say shadowMC).
If you added it dynamically then use,
yourDynamicMC.name = "shadowMC"
If added manually on the stage then give instance name as "shadowMC",
Then, inside your code where you have MouseEvent function for parent MovieClip add the following lines
if(e.target.name != "shadowMC")
{
//Then proceed
}
Reading your question, I am assuming you already tried to set the parent MovieClip's mouseChildren to true with mouseEnabled to false and then set the children's mouseEnabled to true (except for the shadow). This solution should work in my opinion so I am guessing the event might be caught by one parent of your movieclip (you do not give much information about this).
Try to add a listener to the stage to see which object is receiving your MouseEvents:
import flash.utils.getQualifiedClassName;
stage.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(event:MouseEvent):void
{
trace(event.target.name, getQualifiedClassName(event.target));
}
Try changing the hit area of your MovieClip. see the MovieClip doc here, this will override you MovieClip zone.

AS3 addChild() drawing on top of my movieClip on CLICK

So basically my issue is when I click on my movieClip I want it to spawn this animation I did over the protonCore. Essentially to show that when you click the protonCore you generate 1 proton. However the problem with this when spamming your CLICK, is that when it adds this child on every click, it draws on top of the movieClip and prevents hit detection while the addedChild "fuseSpark" is added to the stage. Is there a way to make it so when I add this child it doesn't affect the hitBox of the clickable movieClip?
function protonGenerator(e:MouseEvent):void
{
var fuseSpark:MovieClip = new MC_FX_fuse;
stage.addChild(fuseSpark);
fuseSpark.x = stage_protonCore.x;
fuseSpark.y = stage_protonCore.y;
}
An easy solution would be to disable the mouse for those children when you create them:
fuseSpark.mouseEnabled = false;
That is, ofcourse, only if you don't care if the user can click those elements.

How to set mouse click to do not hit transparent pixels

Imagine a painting program. I have two imagens overlaping each other. I must be able to click on the image behind if I click on a transparent part of the above one.
I add an event listener on each image. So I must prevent the first one to dispatch click event in order to the behind one dispatch it.
(I mean, I already check for transparent pixels, but i can't cancel that event to the other imagem dispatch it.)
There is some dirty solution. Say your "images" are encapsulated in Sprites, otherwise you can't attach a listener to a Bitmap object or a Shape object. MovieClips - well, should still work, although checking alpha is a lot harder. You have your listeners attached to those sprites. First, that sprite in itself can check transparency, and if not transparent, proceed the event. To get what's behind, you can call stage.getObjectsUnderPoint(), this will return an Array of DisplayObjects, with foreground being on top. So you might have one single listener that would call this to enumerate what's under the point, then do as Roman Trofimov advises to determine that object's transparency. Once you'll find a non-transparent object, you do:
// "b" is a non-transparent DisplayObject found beforehand
while (b && (!(b is InteractiveObject))) b = b.parent;
if (b) {
var p:Point = new Point(e.stageX, e.stageY);
p = b.globalToLocal(p);
b.dispatchEvent(new MouseEvent('click', true, true, p.x, p.y));
}
This will dispatch a click event to the object that's able to receive events, and is opaque enough to obscure the background.
The MovieClip property 'mouseEnabled' can prevent click events from triggering on objects in front of other objects, for example, take two movie clips of the same size and same position:
mc1.addEventListener(MouseEvent.CLICK, click1);
function click1(e:MouseEvent) : void
{
trace("1");
}
mc2.addEventListener(MouseEvent.CLICK, click2);
function click2(e:MouseEvent) : void
{
trace("2");
}
mc1.mouseEnabled = false;
Output would be "2" as the click would essentially go 'through' mc1.
In flex you can add mouseEnabledWhereTransparent="false" to components. Not fully sure if that variable is available to you though
Generally, you will always get first event from first sprite (if it was not capture phase ot priority change). But as I understand, you need to check was it transparent and if not - stop bubbling this event.
To stop event - use event.stopImmediatePropagation() in event handler.
To determine transparency:
1) If it is just only the one bitmap, you can check its .bitmapData with method .getPixel32, it returns you alpha value.
2) If it is combined sprite (vector and bitmap), you need to manually render it to Bitmap using BitmapData.draw and then use solution from second case
3) To understand if it was absolutelly miss click, you can try this method: Check transparency

How to click through a Rectangle (like mouseChildren = true)

I have this problem, I use this image pan class: http://www.lextalkington.com/blog/2009/08/auto-pan-class-for-panning-an-image-on-mouse-movement/
but the problem is that the objects/sprites/movieclips that are in it have to be clickable, only problem is that the mouseChildren adn mouseEnabled properties can't be applied to a Rectangle object.
Anyone has an idea on how to be able to click through this so I can acces my objects in the panned item? (if that makes any sense...)
This class is using a Rectangle as the scrollRect for the image. The scrollRect only specifies the visible area of the image. It is not the thing you want to detect mouse clicks on.
Instead, you can listen for a mouse click on the image itself.
From the code you linked to, the image is a DisplayObject variable named _clip.
In the constructor for that image panning class, you can add your mouse listener:
_clip.addEventListener(MouseEvent.CLICK, onImageClick);
Then define the event handler:
private function onImageClick(event:Event):void
{
// do something
}
By the way, since _clip is a DisplayObject, it doesn't have mouseChildren or mouseEnabled properties (those are defined in subclasses of DisplayObject).
_clip.mouseEnable = false;
this should work, considering tha _clip will be clicked through