Disable Focus - AS3 - actionscript-3

I'm relatively new to Flash, and I was just wondering if there was any way to keep the focus off certain elements (specifically, a button, but also input text areas and such alike).
It's nothing to drastic, but it annoys me having the tab key focus on a button rather than other things. I'd rather just be able to disable the focus on that object permanently if that possible.
Thank you so much!

To disable that your component (object) get the focus when the Tab key is pressed, you can use the tabEnabled property and set it to false, like this :
// button control
var button:Button = new Button(); // fl.controls.Button
button.tabEnabled= false;
addChild(button);
// input text field
var text_field:TextField = new TextField();
text_field.type = 'input';
text_field.tabEnabled = false;
addChild(text_field);
Also, for a UIComponent like fl.controls.ColorPicker, fl.controls.ComboBox, ..., you can use the focusEnabled property like this :
// color picker control
var color_picker:ColorPicker = new ColorPicker(); // fl.controls.ColorPicker
color_picker.focusEnabled = false;
addChild(color_picker);
// slider control
var slider:Slider = new Slider(); // fl.controls.Slider
slider.focusEnabled = false;
addChild(slider);
Hope that can help.

Related

Update text field into movie clip from main timeline code

I'm working in a project for basketball. I have an issue, all my code works great if all my components are in the main timeline.
But as soon as I convert the text fields into a movie clip so I can animate and apply alpha value, all stops working.
what am I doing wrong ? The only solution that I could think of is writing the result of my countdown into the text field in the movie clip, but it didn't work as well.
this is my code.
function onTimer ( ev:TimerEvent ) : void {
timeRemaining--;
if (timeRemaining < 0) {
timeRemaining = 0;
loseGame();
}
else
showTime.text = formatTimeRemaining ();
var miReloj:MovieClip;
var titulo_txt:TextField = new TextField();
titulo_txt.text = formatTimeRemaining ();
addChild(miReloj);
miReloj.addChild(titulo_txt);
// miReloj.addChild(showTime1.text);
//miReloj.showTime1.text = formatTimeRemaining ();
}
There's never a value being assigned to miReloj, that's why it's null
var miReloj:MovieClip; // no assignment here
var titulo_txt:TextField = new TextField();
titulo_txt.text = formatTimeRemaining ();
addChild(miReloj); // miReloj is still null here
miReloj.addChild(titulo_txt); // cannot call method on null
But as soon as I convert the text fields into a movie clip
That's impossible. From your code it looks like what you actually want to do is to add the TextField object to a container. There's no need to use a MovieClip for that, simply create a Sprite object:
var container:Sprite = new Sprite(); // create container
var title:TextField = new TextField();
title.text = formatTimeRemaining();
addChild(container); // add container
container.addChild(title); // add title to container
Always use English for all your programming. Don't mix other languages into it. It becomes a pain to read your code and decreases the number of people that can help you if you have problems with your code.

AS3 Creating a "Text Tool"

So i'm developing an application in which allows users to Type on the stage. The text needs to appear wherever the mouse cursor is positioned (and the text would follow the mouse cursor if moved). For the most part it works pretty well. I have two questions.
1) How would I allow the text to follow the mouse cursor? My current code only allows users to type when they click down on the stage.
var textfield = new TextField();
textfield.type = TextFieldType.INPUT
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.defaultTextFormat = textformat;
textfield.textColor = selectedColor;
textfield.x = mouseX;
textfield.y = mouseY;
stage.focus = textfield;
textfield.selectable = false;
/* Add text to stage*/
board.addChild(textfield);
My second question, how would I clear the text? The text would be "pasted" down onto the canvas, so using the "" method would not work. I want an "eraser" to erase the text from the stage. It would be just like stage.graphics.clear but just for text.
Any help I could get would be greatly appreciated. I am still very new to AS3 and I think I gave all the information needed. Thanks.
To make your text follow the mouse cursor, you can add an event listener to the stage:
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
function onMouseMove(event:MouseEvent){
textfield.x = event.stageX;
textfield.y = event.stageY;
}
to make it stop following, you can remove the event listener:
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
to clear the text, you call textfield.text = "";

put a "back" button in front on an embed map

I've integrate a map from MapQuest in my Adobe Air app (in AS3).
The map is taking all the screen as I want.
BUT, I'd like to add a "back" button in order to go back to the previous menu.
Here's my code :
var NoumeaNord:TileMap = new TileMap("KEYcode");
//set the size of the map
NoumeaNord.size = new Size(800, 533);
//add the map to the sprite.
addChild(NoumeaNord);
NoumeaNord.addControl(new SMLargeZoomControl());
NoumeaNord.addControl(new MouseWheelZoomControl());
NoumeaNord.addShape(new Poi(new LatLng(-22.2758000,166.4580000)));
NoumeaNord.setCenter(new LatLng(-22.2758000,166.4580000),15);
function addBackBtn():void{
var back:MovieClip;
back = new backBtn
addChild(back);
back.x = 0;
back.y = 400;
setChildIndex(back,0);
}
Don't know why but the BackBtn won't be in front of the map !
I've tried with setChildIndex(back,-1); but it makes an error : "RangeError: Error #2006: index is off limit".
Any idea ?
In this situation I use holders like this:
private var bgHolder:Sprite;
private var contentHolder:Sprite;
private var menuHolder:Sprite;
Somewhere in the constructor I run a method that will set my holders. For example:
function setHolders()
{
bgHolder = new Sprite();
addChild(bgHolder);
contentHolder = new Sprite();
addChild(contentHolder);
menuHolder = new Sprite();
addChild(menuHolder);
}
Then I just add my content to the desired holder in any order between destination holder and I can always be sure to add my content to the correct "layer";
So just add your back button to the menuHolder:
menuHolder.addChild(back);
And don't worry about adding it below the map or out of index, because the map, in this case, would be added to the contentHolder!
Hope that helps!
that's because you add your back button first
just make sure that you first add the map then add the button

Swapping the image used for my character, to a seperate image on button click

So first off i'd like to state that i am not very good at AS3, i'm completely self taught so i am sure that there are many things i've done badly, inefficiently or plain wrong and i'm happy for any comments on these if there is things i can improve.
Okay so, this seems like a really simple problem but i don't know the exact code for it and i can't find an example anywhere
what i want to do is to change the image which i am using for my character to a separate image on a button click, then after a certain amount of time for him to go back to the original.
My image is set as a movie clip and i am calling it like so :
public var Smallclock_hero = new Smallclock;
it is worth noting that smallclock has it's own separate class.
is there a way that i can change this image on a mouse click event ?
A simple way would be:
Add a new image on the second frame of the Smallclock movieclip. Have this new image span several frames of the timeline for the duration you want it to appear for.
Place a stop(); action on the first frame of the Smallclock movieclip.
btn.addEventListener(MouseEvent.CLICK, btnChangeCharacterFrame);
function btnChangeCharacterFrame(e:MouseEvent)
{
Smallclock_hero.gotoAndPlay(2);
// Or use a frame label
// Smallclock_hero.gotoAndPlay("jump");
}
This can be achieved relatively simply using the visible property and setTimeout. When you click on your MovieClip you'll want to make the old image invisible and the new image visible:
oldImage.visible = false;
newImage.visible = true;
Then you'll want to use setTimeout to change the visible properties back to:
oldImage.visible = true;
newImage.visible = false;
setTimeout can run a function after a specified number of milliseconds. Here is an example of this technique on wonderfl : http://wonderfl.net/c/acxl
private var imageA:ImageA = new ImageA();
private var imageB:ImageB = new ImageB();
private var revertId:int;
public function Main() {
imageB.visible = false;
addChild(imageA);
addChild(imageB);
addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void{
clearTimeout(revertId);
revertId = setTimeout(revert, 1000);
imageB.visible = true;
imageA.visible = false;
}
private function revert():void{
imageB.visible = false;
imageA.visible = true;
}

Convert TextArea into TextField in AS3

I've a TextArea component in my MovieClip. When I double click on it, I want to switch to TextField component, allowing me to change its content. When I click outside, I want to restart its original class (TextArea).
How can I do it?
I'm doing this, but didn't work:
element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName);
private function changeName(e:MouseEvent):void{
e.target.type = TextFieldType.INPUT;
}
Where element is a TextArea (clasic and dynamic text). Thanks!
EDIT:
This is how my MovieClip looks. "Name" is the TextArea that I want to allow user changes. I'm setting it like this:
[Spanish interface]
Nombre de instancia = Instance name (empty)
Texto clásico (classic text)
Texto dinámico (dynamic text)
The MovieClip is controlling my by own base class (called 'ConfLayer'). Inside it I have this:
public function doStuff(e:MouseEvent):void{
// element = TextArea 'Name'
element.addEventListener(MouseEvent.DOUBLE_CLICK, changeName);
}
private function changeName(e:MouseEvent):void {
var tarea:TextArea = e.target as TextArea;
var tf:TextField = tarea.TextField; // this line throwing error
tf.type = TextFieldType.INPUT;
}
Because AS3 gives me errors, I tried this:
private function changeName(e:MouseEvent):void {
e.target.TextField.type = TextFieldType.INPUT;
}
When I double-click on TextArea element, the previous string removes and I can't write nothing.
You make field editable only after second click. My idea is that it's to late. the field is editable, but you'll need to click again. So, try to make ediable for a while (e.g. 300 mileseconds) after the first click. If there's no click you set the type back to DYNAMIC. After second click you'll start editing text field.
Following on my comment above, a TextArea has a textfield property.
So you should be able to do something like this:
private function changeName(e:MouseEvent):void{
var tarea:TextArea = e.target as TextArea;
var tf:TextField = tarea.textField; //make sure that textfield is camelCase!!!
tf.type = TextFieldType.INPUT;
}
I solved my own problem using background and border properties from TextField. Instead of create TextField directly on my MovieClip, I create it dynamically.
// Field 'Name' of the layer
var tarea:TextField = new TextField();
tarea.text = 'Layer';
tarea.x = -80;
tarea.y = -23;
tarea.type = TextFieldType.DYNAMIC;
tarea.height = 20;
// Format the TextField
var format_tarea:TextFormat = new TextFormat();
format_tarea.font = "Arial";
format_tarea.size = 14;
format_tarea.bold = true;
tarea.setTextFormat(format_tarea,0,tarea.length);
Then, I add it some listeners to allow changes when I click on it:
// Add event to allow name changes
tarea.addEventListener(MouseEvent.CLICK,changeName);
When I press ENTER key, I accept the changes
// Add event to accept changes on press ENTER key
tarea.addEventListener(KeyboardEvent.KEY_DOWN,acceptKeyboardName);
When TextField lost his focus, accept the changes too:
tarea.addEventListener(FocusEvent.FOCUS_OUT,acceptFocusName);
Last, I added to my own MovieClip
// Add to MC
mc.addChild(tarea);
Handlers associated to below events are these:
private function changeName(e:MouseEvent):void
{
e.target.setSelection(0,e.target.text.length);
e.target.border = true;
e.target.background = true;
e.target.type = TextFieldType.INPUT;
}
private function acceptKeyboardName(e:KeyboardEvent):void
{
if (e.charCode == 13) // When pres ENTER, remove border, background and restore dynamic type
{
e.target.type = TextFieldType.DYNAMIC;
e.target.border = false;
e.target.background = false;
}
}
private function acceptFocusName(e:FocusEvent):void
{
// When lost focus, remove border, background and restore dynamic type
e.target.type = TextFieldType.DYNAMIC;
e.target.border = false;
e.target.background = false;
}