AS3 Creating a "Text Tool" - actionscript-3

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 = "";

Related

Disable Focus - AS3

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.

Flash ScrollPane confusion, scrolling down scrolls to the right

I have a confusing ScrollPane issue that I hope someone has encountered before!
I have a movie clip called Inner_Area, which gets a number of other smaller movie clips loaded into it. The end effect is that it looks like a list of strings.
After the Inner_Area movieclip is populated with its additional information, I set the source of a ScrollePane object to it.
scroll_area.source = Inner_Area;
When I play the movieclip, the list loads up successfully, and a scroll bar appears on the right hand side. This is where the strange part happens.
If I click the scroll down arrow, to try to see the items at the bottom of the list, the Inner_Area actually seems to scroll to the RIGHT. Not down at all. I've looked over my actionscript and I cannot for the life of me see how this could be.
I've not included any code, of course, because I am initially hoping that someone may have experienced a similar situation before. How can I get a scrollpane area such that when you scroll down, it actually goes down instead of in a different direction?
Any hints, tips, or advice is much appreciated!
Some Code:
public class MenuBackground extends MovieClip
{
scroll_area = new ScrollPane();
scroll_area.x = -275;
scroll_area.y = -77;
scroll_area.width = 250;
scroll_area.height = 175;
addChild(scroll_area);
inner_area = new Inner_ZoneScrollArea();
spacing = 0;
for (i= 0; i < numthings; i++)
{
_field = new _Field();
_field.y = spacing;
spacing = spacing + 20;
inner_area.addChild(_field);
}
scroll_area.source = inner_area;
}
And in a different file, is the _Field code:
public function _Field()
{
_Format = new TextFormat();
_Format.size = 16;
_TextField = new TextField();
_TextField.x = 50;
_TextField.y = 4;
_TextField.textColor = 0xFFFFFF;
_TextField.defaultTextFormat = _Format;
_TextField.autoSize = "left";
_TextField.multiline = true; // Just added these last two on suggestion in this thread
_TextField.text = "Name"; // some text
addChild(_TextField);
}
not sure if you have got this one fixed yet mate but I think I had a similar issue trying to add Dynamic content to a ScrollPane if your having an issue with the scrollbars not coming up you need to do an update to the ScrollPane I had to put mine on a Timer so that it gave the content time to append and then update. like so
var timer:Timer = new Timer(3000,1);
timer.addEventListener(TimerEvent.TIMER, updateEvent);
timer.start();
function updateEvent(e:TimerEvent):void
{
ScrollPane.update();
timer.stop();
}
hope this helps any one else with a similar issue loading Dynamic content into a ScrollPane
The following simple example produces a ScrollPane that scrolls down:
var tf:TextField = new TextField();
tf.text = "Lorem \nIpsum dolor \nsit amet \nthese are \nmany\nmany \nmany \nmanylines of \ntext";
tf.autoSize = "left";
tf.multiline = true;
var sp:ScrollPane = new ScrollPane();
sp.source = tf;
addChild(sp);

movie clip on click at mouse coordinates prevent mouse click

I want to add at each click a movie clip (it's smoke, as the pointer is a gun).
I've managed to do that, BUT now I can't clik on anything (buttons,objects..etc). The smoke appears juste fine but seems to "block" the click.
Do you know how I can resolve this issue ?
Here's my code :
private var fumee:MovieClip;
stage.addEventListener(MouseEvent.MOUSE_DOWN, tire, false, 0, true);
public function tire(e:MouseEvent):void{
fumee = new fumeeFusil;
addChild(fumee);
fumee.x = mouseX;
fumee.y = mouseY;
}
Thank you,
You need to make sure your fummeFusil instances are not mouse-accessible:
public function tire(e:MouseEvent):void{
fumee = new fumeeFusil;
// setting both mouseEnabled and mouseChildren to false will ensure
// that you cannot click on the MovieClip
fumee.mouseEnabled = fumee.mouseChildren = false;
addChild(fumee);
fumee.x = mouseX;
fumee.y = mouseY;
}

AS3 - Trace - Click button

I am trying to click on a button and trace out a number "1" onto the stage/same scene.
I have this code:
button1.addEventListener(MouseEvent.CLICK, myClickReaction1);
function myClickReaction1 (e:MouseEvent):void{
trace("1");
}
BUT it traces onto the output of flash and not onto the scene.
please help
thanks
trace("1"); will go to the output panel it is intended for debugging.
If you want to see something on the stage, you will need to create a TextField and set it's text property to whatever you want.
button1.addEventListener(MouseEvent.CLICK, myClickReaction1);
function myClickReaction1 (e:MouseEvent):void{
var tf:TextField = new TextField();
tf.text = "1";
addChild(tf);
}

Unselect the select item in as3

First of all, I create txtfield at _txtbtn. then i select the any mc and create the many listener on them. And now I want to remove listener when I click on _can. So I created _unselect function. But it doesn't work as expected.
function _txtbtn(e:*):void
{
myText = new TextField();
mc3 = new MovieClip();
myText.text = "text...";
myText.type = "input";
mc3.addChild(myText);
addChild(mc3);
mc3.x = _can.x;
mc3.y = p;
p = mc3.y + mc3.height + 10;
this.mc3.addEventListener(MouseEvent.MOUSE_DOWN,_select);
function _select(e:MouseEvent):void
{
tool_stage.combo.addEventListener(Event.CHANGE,_font);
tool_stage.steeper.addEventListener(Event.CHANGE,_size);
tool_stage.italic.addEventListener(MouseEvent.CLICK,_bold);
tool_stage.colPicker2.addEventListener(ColorPickerEvent.CHANGE, changeColor1);
_can.addEventListener(MouseEvent.MOUSE_DOWN,_unselect);
}
function _unselect(e:*){
mc3.removeEventListener(MouseEvent.MOUSE_DOWN,_select);
}
My gut is telling me that you may have registered more than one mouse down listeners to "mc3". Without knowing the full scope of the code, I suggest that you add this following line before you add the mouse down listener to mc3.
// This is the new line, it is just to remove any prior event registrations just to be safe.
this.mc3.removeEventListener(MouseEvent.MOUSE_DOWN,_select);
//This is the original line of the code, leave it as is
this.mc3.addEventListener(MouseEvent.MOUSE_DOWN,_select);