AS3 addChild() drawing on top of my movieClip on CLICK - actionscript-3

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.

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

As3 (code snippets) change property of object on stage from within another object

I've been searching for the past few hours how to do this but with no luck. First of all I'm new to AS and not sure what to search for.
So here's what I have and what I want to do:The stage has only 1 frame and on the stage I have 1 button (b1), 1 movie clip that is not visible (area) and 1 visible movie clip that is an animation of 20 frames(ani). For the moment when I press button "b1" it will start the movie "ani" (movie is stopped initially) that will stop when it reaches frame 20. Now what I want is when it reaches the last frame to make movie clip "area" visible. Since I am inside "ani" and on frame 20, I cannot use directly area.visible = true; as I would get the error "Access of unidentified property area." What would be the way to access "area"'s properties from within the other object ?
Inside your "ani" MovieClip (at last frame) add the following sentence,
MovieClip(this.parent).area.visible = true;
Here, parent is your main timeline.
(Note: This approach is not recommended).
Instead, use External classes approach. e.g. use Loader class to load animated swf with COMPLETE event and with contentLoaderInfo get swf object and detect for last frame and make area MovieClip visible.
I'm not sure I completely understand everything you said, but I think what you want to do is something like this on your stage where both ani and area have scope :
ani.addEventListener(Event.ENTER_FRAME, frameCheck);
function frameCheck(e:Event):void
{
if (ani.currentFrame == ani.totalFrames)
{
ani.removeEventListener(Event.ENTER_FRAME, frameCheck);
area.visible = true;
}
}
That was just an example of how you could detect ani hitting the last frame and dealing with that appropriately.
You would need to add the event listener EACH TIME the button is pressed.

AS3 setChildIndex to front

Is there a way to send a specific movieClip to the front of all other movieClips on stage?
I know about setChildIndex, but I can't figure out a way to to calculate the top position dynamically.
You can use setChildIndex() with numChildren.
setChildIndex(childClip, numChildren - 1);
You don't need to use setChildIndex.
addChild is sending child on the top of all clips.
You may be thinking that addChild will double add your MC, but it will not, the second time it will just update it's child index.
By default, flash adds the movieclip to the top of the display list, so you can just repeat adding of the child to the container.
stage.addChild(bottom);
stage.addChild(top);
// top is now on bottom.
stage.addChild(bottom);
// bottom is now on top.
The movieclip(mc) is added on the canvas,
canvas.setChildIndex(mc, 0);
The canvas is added on the stage,
stage.setChildIndex(canvas, 0);
/* Bring Any Clicked Object to the Front
Clicking on any symbol on the Stage moves it in front of all other instances.
*/
// This code makes all symbol instances on stage clickable by making them listen for the CLICK event.
for (var fl_ChildIndex:int = 0;
fl_ChildIndex < this.numChildren;
fl_ChildIndex++)
{
this.getChildAt(fl_ChildIndex).addEventListener(MouseEvent.CLICK, fl_ClickToBringToFront);
}
// This is the function that moves the clicked object to the front of the display list
function fl_ClickToBringToFront(event:MouseEvent):void
{
this.addChild(event.currentTarget as DisplayObject);
}
From Adobe Flash Professional Code Snippets.

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

MoveClip.Visible and MouseEvent - ActionScript3

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