AS3: Managing two TextInputs - actionscript-3

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).

Related

How to check if movie clip or any of it's children lose focus in AS3?

I have made custom Class for Input TextField with caption and own virtual keyboard which is loading in same MovieClip where TextField resides. And all works just fine. I wanted to remove keyboard from stage (from MovieClip stage) when user clicks on other visible input TextField, where new keyboard will be displayed.
If I check losing focus from MovieClip, wherever I click on keyboard (but not into Input TextField), it is closed because stage loose focus. Is there any way to check if any of children has no focus, so I can in that moment close the keyboard? In other word, to check if focus completely moved out from MovieClip stage to another.
I suppose you're just looking for FocusEvent In case you're need to check focus on MovieClip's children, you're have to attach listener to all of them.

Is it possible to make an object "Transparent" to mouse click?

Im developing a flash game, and i would love to implement raining effect. Here's my progress on rain so far: http://www.squ4re.eu/Rain.html
The code is pretty simple; every raindrop is an object, when it hits the ground it places itself again at the top of the screen and adds splash animation.
But the problem is to click something BEHIND the rain. Lets say i have some selectable units at the battleground. In most cases an random raindrop interrupts selecting an object behind it. So here's my question: Is it possible in flash to create object "transparent" to mouse click, so i can click an object behind it? Or is there any other way to solve this problem?
Thank you in advance.
As #putvande mentioned, you could use mouseEnabled on every interactive object that should be disabled for mouse interaction. You also could create rainLayer and disable it for mouse interaction:
myRainLayer.mouseEnabled = false;
myRainLayer.mouseChildren = false;
mouseChildren - determines whether or not the children of the object are mouse, or user input device, enabled. If an object is enabled, a user can interact with it by using a mouse or user input device. The default is true.
Also consider to use display objects that don't inherit from InteractiveObject, like Bitmap, Shape and Video

As3: Button only working in certain locations?

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.

AS3 deactivate (MovieClip) Buttons

I want to temporarily deactivate some movieclips that are used as buttons. Currently I don't keep track of the EventListeners that got added to these buttons. I want to be able to deactivate and reactivate them later.
myMc.mouseEnabled=false;
works, but then they are still select- and clickable using the TAB key on the keyboard.
how to prevent that?
MovieClips have a property called enabled which prevents the MovieClip featuring in the tab order. Use this is conjunction with mouseEnabled to completely disable a MovieClip.
In AS3 MovieClips no longer appear or behave as buttons even when they have listeners although they would work just fine when clicked or rolled over with mouse, you have to specifically enable the button mode for MovieClips to make them change cursor to hand:
buttonMC.buttonMode = true;
buttonMC.useHandCursor = true;
To disable the button completely, remove the listener for each event you want it to stop working and also disable the button mode for the MovieClip:
buttonMC.removeEventListener(MouseEvent.CLICK, onClickHandler);
buttonMC.removeEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
buttonMC.removeEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);
buttonClip.buttonMode = false;
source: http://www.parorrey.com/blog/flash-development/how-to-enabledisable-movieclips-as-buttons-in-flash-with-actionscript-3-0/
Or a quick way would be to hide the movieclips when not required
myMc.visible = false;

As3 - Child heredit event listener. How to stop?

I'm an as3 newbie. I'm experiencing this strange problem. I've created a button (type: MyButton) with two child, a text (TextField) and a image icon (type: MyIcon).
Then I've append an eventlistener mouse_click on my button.
As soon as I click on the text, the e.target on the handeler function is recognized of MyButton type. Otherwise, if I click on the image icon (child of button) the e.target is MyIcon type, instead of MyButton.
How can I prevent this? I need all click to be recorded on the button, where I've stored some attributes I need on handeler function.
Thanks.
Use the e.currentTarget instead. It returns the object that the MouseEvent has currently bubbled to. e.target returns the object that the MouseEvent actually started on.
Read about Event bubbling here. It's a very important concept to grasp.