AS3 InputTextField Component size change - actionscript-3

I want to use text input component and I drag it on the stage, I give it a instance name. Now from code I'm trying to change the size of it by instance name , because I expand a little bit and the text size remain like it was.
Here is my code:
userLog.size = 30;
I receive this error:
1119:Access of possibly undefined property size through a reference with static type fl.controls:TextInput.
Thank you!

You can do:
userLog.width = 30;
Or even:
userLog.width = userLog.textWidth ;
Or even more:
userLog.addEventListener(Event.CHANGE,onChange);
function onChange(e:Event):void{
var new_width:Number;
if(userLog.textWidth<100){
new_width = 100;
} else {
new_width = userLog.textWidth;
}
userLog.width = new_width;
}
UPDATE. In case you want to change TextInput's font size , use TextFormat:
var format:TextFormat = new TextFormat();
format.size = 24;
userLog.setStyle("textFormat", format);

Related

as3 change font size as stage size changes

As it reads I want to change the font size as the stage size changes.
I have private var myFormat:TextFormat = new TextFormat();
And then when the object gets made it writes the following
_buttonText.wordWrap = true;
myFormat.size = 15;
myFormat.align = TextFormatAlign.CENTER;
_buttonText.textColor = 0x222428;
myFormat.bold = 'true';
_buttonText.defaultTextFormat = myFormat;
_buttonText.text = text;
Then in my on enter frame I want to resize the text, I have tried a couple things but nothing seems to work, currently it looks like this.
myFormat.size = stage.stageWidth / 136.53;
Thanks for any help
A TextFormat object has no effect unless applied to a TextField. Besides if the font size should be linked to the stage size then a factor size of some kind should be applied as well. At the end it looks like this:
myFormat.size = 15 * fontSizeFactor;
//_buttonText.defaultTextFormat = myFormat;this is irrelevant if size should be dynamic.
//instead text should be set then TextFormat should be set again.
_buttonText.text = text;
_buttonText.setTextFormat(myFormat);//this is dynamic
now on enter frame:
myFormat.size = 15 * fontSizeFactor;
_buttonText.setTextFormat(myFormat);//apply the format again or else nothing will happen

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader

AS3 - setTextFormat(), defaultTextFormat not working as designed?

UPDATED with simpler snippet
I am building a Flash swf to check through some XML files using AS3.
I only have access to the Flex compiler, not the actual Flash IDE or Flashdevelop, so all my xml checking code is a single AS file, compiled into an swf. In order to display information, I create and add a TextField and call appendText() (I use += in this snippet).
The snippet below seems to not set the new format on existing text. It appears to not have any effect whatsoever on the text.
I was under the impression that setTextFormat would change the existing text and that any new text added after calling setTextFormat would use the defaultTextFormat object. This does not appear to be the case in my code. Am I incorrect in my understanding?
public function AS3Tester()
{
display_txt = new TextField();
display_txt.multiline = true;
display_txt.width = 1024;
display_txt.height = 768;
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.font = "Lucida Console";
display_txt.defaultTextFormat = tf;
display_txt.text = "Text Before setTextFormat\n"; //Should use default style tf
addChild(display_txt);
var tf2:TextFormat = new TextFormat();
tf2.size = 18;
tf2.color = 0xFF0000;
display_txt.setTextFormat(tf2);
display_txt.text += "Text after setTextFormat\n"; //previous line should use tf2, this new line should use default tf
}

AS3 multiple textfields made easy

I am working on a Results page for my game as well as upgrade page and looking for an easy way to do many textfields. I have a format for my text that takes care of font, colour, and size, but looking for an easy way to do the width and height of textfields to increase all at the same time.
I have been informed about a "with" keyword that may work but do not understand how to implement this within my program and essentially want to shorten my results class if possible.
Thank you,
The best way would be to create a custom function for generating textfield.
The example can be found in the livedocs itself.
So something like the following should suffice :
private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x;
result.y = y;
result.width = width;
result.height = height;
return result;
}
You may also set a default value to each attribute in the function.
private function createCustomTextField ( x:Number= <Default Value>, ...
Use it to add a textfield inside the container form.
var container:Sprite = new Sprite(); // New form container
container.addChild(createCustomTextField (20,20,50,50)); // Text Filed 1
container.addChild(createCustomTextField (20,50,50,50)); // Text Filed 2
addChild(container); // Add to current class
You may want to modify the function to accept a name so that each variable can be accessed later.
As far as I am aware, you can't use a "with" keyword to target multiple objects. Here's the documentation for it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#with
What I've done in the past is just make an array of all the targets, and then write a loop to apply properties to each:
var textFormat:TextFormat = generateMyCustomTextFormat();
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
//...
var textField3:TextField = new TextField();
var targets:Array = [textField1, textField2, textField3];
for(var i:int=0; i<targets.length; i++)
{
targets[i].defaultTextFormat = textFormat;
targets[i].width = 250;
//...
}

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide