As3: Button only working in certain locations? - actionscript-3

I really don't know how to explain this problem. I'm really stumped as to what is causing it.
Here is the code:
var abutton:AButton = new AButton; //Where AButton is a button defined in my library
addChildAt(abutton, numChildren);
abutton.addEventListener(MouseEvent.CLICK, attack);
It doesn't want to work when certain movie clips are underneath it, but I don't want to make it more complicated by switching to another screen. Is it possible to make the button work with movieclips underneath?

What do you mean by "certain movieclips"? What do you mean by not working? The CLICK event isn't firing? Normally if a click isn't working on a button, it means that something else it trapping the mouse click above your button. This can be another Sprite, MovieClip or TextField.
Add a click listener to the stage, and have it print out target and currentTarget. Then, when you button doesn't work, the stage listener will still fire and you'll be able to see the object that's blocking your button.

Related

Actionscript 3 addchild to a button

I am trying to achieve sort of like an "extender" to a button. I want it to be like a invisible mask, and when you click on it, its the same as clicking on a button directly. Is this possible with an addchild method? Also, if possible, without having to make extra addeventlistners for the mask, so that additional object is seen as that same button. I know it can be done using a simplebutton, but what about a fl.controls button?
You an use either a Sprite or Movieclip as clickable button.
Create both the "button" part and the "mask" part within same MovieClip, and give that whole MC one event listener.
Example if called: myMClip_Button...
myMClip_Button.addEventListener(MouseEvent.CLICK, handler_Mouse_Click );
Later, to access / modify anything within the MovieClip, just use a path:
myMClip_Button.myButton = something;
myMClip_Button.myMask = something;

AS3: Managing two TextInputs

I am pretty new to AS3 and I'd like to learn from the more experienced ones how to do it right. The problem I have is: having two text inputs, having the ability to change the focus from one to another and the most important one, make the input lose focus on click outside.
The problems I faced here are:
When I click outside text inputs, it does not loses focus
If I focus in a text input, minimize browser and come back, it auto refocuses the last element.
How do you see this process implemented and what could I do to solve the problems I face?
To drop focus on your textfields, set a mouse event on the stage:
stage.addEventListener( MouseEvent.CLICK, onDropFocus );
function onDropFocus( evt:MouseEvent ):void
{
Stage.focus = null;
}
To reset the proper textfield focus, store a reference to it when you focus in on your textfield, then try listening for Event.DEACTIVATE on your stage, which is triggered when the flash movie loses focus. Then you can refocus to the intended textfield before leaving the page, (like when minimizing).

Why isn't this click event being fired?

Why isn't the myMouseClick event being fired?
myMC:TestMC = new TestMC();
myMC.addEventListener(MouseEvent.CLICK, myMouseClick);
addChild(myMC);
function myMouseClick(e:MouseEvent):void {
trace("clicked");
}
As far as I can tell from the tutorials I've seen, that should work. For a moment, I thought that since I was adding the event listener to myMC, I needed to have the event function inside the myMC class, but that didn't work. Just gave an error about accessing an undefined property.
If it helps any, TestMC is a seperate .as file that extends movie clip.
I'm just trying to make it so when the movie clip itself is clicked, it does something. The movie clip itself will be following the mouse.
The object I was trying to click was made up on vertical lines. Apparently the whole movie clip isn't a collider... just the pixels in it, so when I was clicking, I wouldn't hitting enough of it. Changing it to a box worked. I could probably nest it if I wanted the same design, but that's fine.

actionscript 3 popup window - not js

Im doing a program in AS3 and, i'm trying to do the following:
Lets say I have a button, which have onClick event, and when the event is triggered, the popup window will appear.
And in this popup window there is going to be an animation (countdown) of 2020 (this is just an example).
Is there a way to do this without javascript. Because i'm not doing this for website, its going to be a animation.
Hope i was clear.
Thanks
In Flash,create a movie clip, give it an instance name of "countdown_mc", put the countdown animation inside it.This will be your "alert" type window.
Now, in the movieclip containing "countdown_mc", add a new layer and on the same frame bring up the action editor(F9) and write the following: countdown_mc.visible=false;
When you push the button it should make countdown_mc.visible=true and maybe do an optional growing animation;
Also on this frame add a listener for countdown_mc so that when the user clicks on the window it disappears(countdown_mc.visible=false);
For animations add download tweenmax from http://www.greensock.com/tweenmax/, extract the archive in the folder that contains your fla and add the following to your code:
import com.greensock.*;
TweenMax.from(countdown_mc,0.5,{scaleX:0,scaleY:0});

AS3 Button stops working after random amount of click

I have a movieclip being used as a button. After a random amount of clicks the button stops working. In other words, the mouse will become a hand when hovering over the button but no clicks are registering to fire the function. I've even clicked it 40 times and it will work but then suddenly, bang!, it stops working. Heres the function that adds the btn, listener, animates it into the screen and also adds text.
function makeButton():void{
addChild(myBtn);
myBtn.mouseChildren=false;
myBtn.buttonMode=true;
myBtn.x=(stage.stageWidth/2)-(myBtn.width/2);
myBtn.y=-300;
myBtn.addEventListener(MouseEvent.MOUSE_DOWN, btnClicked, false, 0, true);
myBtn.btn_text.text="The string goes here";
TweenLite.to(myBtn, 0.5,{x:(stage.stageWidth/2)-(myBtn.width/2),y:(stage.stageHeight/2)-(myBtn.height/2)});
}
And then here's the function that animates the button outside the screen:
function btnClicked(e:MouseEvent):void{
myBtn.removeEventListener(MouseEvent.MOUSE_DOWN, btnClicked);
TweenLite.to(myBtn, 0.5,{x:(stage.stageWidth/2)-(myBtn.width/2),y:-300});
}
Strange thing is, I added a trace("listener added") into the 'makeButton()' AT THE VERY END, AFTER THE ADD EVENT. And it traces everytime, even on the times the button stops working. SO i can only assume there are no errors with listener being added. But then why is it not working?
I'm stumped. I thought it could be an event propagation problem. In other words the listener was being added to the target (myBtn) but somehow it was capturing or bubbling wrong but..... then why does it work at all? And for so many clicks?
The truth is out there. Or maybe in here, your insights will be much appreciated.
Where does myBtn get created? I can see right at the beginning of makeButton() that you are adding it the display list but can't see where it actually gets created? Is it already on the stage?
Adding a trace statement in the makeButton function will only tell you that a button is created, it won't say much about the functionality of your button. If you want to check if your button reacts to a click , you need to add your trace statement in the click listener.
According to your description , it sounds like you keep adding the same button to the stage rather than actually clicking the same button.
How often do you call the makeButton function before it stops working? This function looks like it should only be called once. As for the btnClicked function , why do you remove the listener, if you wish to click the button again?
Practically it looks like you should only have your Tweening functionality in your functions, I mean , once the button is created , you need one function to tween the button, then instead of adding the button again, simply call a function to tween the button back in place.
All the rest shouldn't be repeated.
I've fixed the code and the problem hasn't occurred again. The problem must have been that I was running the addChild every time the function called and that was doing something odd to the MC in the display list. I haven't pursued the error by clicking the buttons many times in a row for a minute or two, as I did to make the error happen originally. I think i'll let sleeping dogs lie.
With that said, my code is a lot cleaner with the addChild and other crap running in the initialization function and just sitting above the stage - and then being tweened into position in the 'makeButton' and 'btnClicked' functions (which are now fittingly named 'tweenBtnIn' and 'tweenBtnOut').
Thanks again