Updating Flash textbox text via ActionScript - actionscript-3

I created a text area on my stage using the text tool, then converted it to a MovieClip symbol. I called it ScoreLabel and I clicked "Export for ActionScript". I named the instance of the text area scoreLabel. However, when I do this in my code:
scoreLabel.text = this.score;
it doesn't change. That line is in my Main.as file, which is the document class. How can I change the text shown in this text area using ActionScript? Maybe I need to import a library?
Thanks.

Like I said, don't export for ActionScript - it's not necessary in your case.
You also need to assign a String to the .text property, so you have do something like this if score is a Number type:
scoreLabel.text = score.toString();
Do you get any errors, and is scoreLabel within scope from your document class, or nested in another container?

Related

Edit complex component in Flash IDE in one ActionScript class

I'm new to Adobe Flash. I'd like to create a button or component in Flash Professional using MovieClip and add label to it, and build my own button with special functions.
And i'd like use ActionScript to edit code. I faced a problem: i can not edit all code of my component (that cotains several ui-elements) in one AS3 class. I create empty document, add rectangle to it, then convert it to symbol. Next, i go to Advanced ActionScript 3.0 Settings of IDE, add path to folder where i put my Class.
Then add MyClass to Document class in that window.
But when i add new elements to the document, it doesn't appear in MyClass.as
Is it possible to edit complex component in Flash IDE in one ActionScript class ?
Sorry for a long explanation and for my poor english.
I create empty document, add rectangle to it, then convert it to
symbol.
Make sure symbol is type: MovieClip. Give it a name you prefer (no space is good idea). Further down (see Advanced section?) there is section called Linkage click "Export for ActionScript" and remember the name shown in Class box (you can edit it) and just click OK. No need for folder paths etc.
Lets assume the name used for Class is : myComponent
Now you can use it in your code. To make a document class open the Properties panel (ctrl+F3) and just type a name example Main (no extensions) and press enter and then save FLA and secondly also save the class code (it will ask for a file name so use as Main.as).
Now If you click that pencil icon in properties you will be editing class code (Main.as) for the FLA document and it knows your component if you reference it by code:
public var test_comp : myComponent = new myComponent (); here test_comp is just a quick reference name. Use your own preferred reference name.
You can edit the MovieClip of myComponent by right-clicking and choosing "edit" (not "edit Class"). Now you can add/draw different things and even convert them to Graphic or MovieClip and give them instance names. A MovieClip object is like a mini-stage so it has its own unique timeline and you can work exactly like if this was the main stage (but without actually affecting main stage).
If you add something to Component MC such as drawing a circle (as some button?) and convert that circle to MC or Graphic type with instance name circle1, in the code you access that as:
test_comp.circle1.alpha = 0.5; since test_comp is your code's reference name for that myComponent MC in the Library.
After testing the above example... now begin your own component.
Remember... If an object is inside the MC access it via it's instance name and if you need to addChild it from the Library instead then use its AS3 linkage name.

Actionscript 3 - Accessing a variable on the main timeline

I'm trying to make a file that is easy for a non-flash user to use/reuse to easily display content. The key here is that this file is to be a template for a novice user to just copy/paste some very minimal code to create different "flash card" type swf files.
The file I am creating has multiple buttons on the main timeline which when clicked, attaches a movie clip which will display a dynamic text area with content specific for the button that was clicked. The content for the text area will be loaded from a separate text file.
For the sake of this example, I'm just going to refer to one button...
So, on the main timeline, in frame 1, I have a variable definition:
var myFilename1:String = "mySampleFile2.txt";
When the button on the main timeline, in frame 1 is pressed, a movie clip is loaded which contains a text area. The content for the text area is located in that file: mySampleFile2.txt.
If I hard-code the file name, it works like a dream:
myTextLoader.load(new URLRequest("mySampleFile2.txt"));
But I don't want to hard code the file name. I want to refer the variable in the main timeline. In AS 2, it would have been
myTextLoader.load(new URLRequest(_root.myFilename1));
In AS3 I thought it would be either:
myTextLoader.load(new URLRequest(root.myFilename1));
OR
myTextLoader.load(new URLRequest(MovieClip(this.parent.root).myFilename1));
When I run the code I get the following error and when I run a trace I get the file name is NULL.
TypeError: Error #2007: Parameter url must be non-null.
How to I access the file name stored in the variable on the main timeline?
*************************** UPDATE! *************************
So I just discovered that the issue is related to a button on the screen. The button is one from the buttons library. If I remove the button, everything works great. But as soon as that button is on the main timeline, it makes it to I cannot access the variables using MovieClip(root).variable_name;. Unfortunately I want that button to trigger the events within the MovieClip. Any thoughts?
Not a good idea
You are not able to achieve your goals with this bad practice approach.
to use/reuse
The code you want to provide is not very reusable. It heavily relies on one variable existing in a certain place. Therefore it cannot be use twice in a project.
But I don't want to hard code the file name.
And now you are hard coding the variable and its location. If you considered hard coding the file name to be a bad things, consider this a bad thing, too.
What the problem is
Basically speaking, the problem is that your component is reaching out to grab this variable from somewhere within your project. But it is not the concern of the component to find the content it should display. It is not well encapsulated.
Learning from existing things
You want to display text. Let's take a look at the TextField class to see how it displays text.
var tf:TextField = new TextField();
tf.text = "hello";
addChild(tf);
As you can see, the text that it should display is passed to the TextField object. There is not some arbitrary variable one has to set in order to modify the text, as you are planning to do:
var tf:TextField = new TextField();
var someArbitraryVariableThatModifiesATextField = "hello";
addChild(tf);
There is no obvious connection to the TextField object and if there's a second TextField, this doesn't work at all.
Applying that to the problem at hand
Just like the TextField, your "flash card" should receive the file as a parameter. Either pass it to the constructor as seen in the example below, or create a method that takes it as a parameter.
var card:Card = new Card("mySampleFile2.txt");
addChild(card);
Additional thoughts
Create additional methods to set the values individually. There's nothing worse than some code that does exactly what one wants, but only operates on files and one doesn't have a file. The goal is again, to make it easy to reuse the code
Use the [Inspectable] meta tag to allow the user of your code to modify the properties at author time. This can be used to the extend of modifying properties belonging to a physics engine, now this is what I would call easy to use for a novice user.
Instead of writing code and thus requiring to recompile the file again, take the information (either the path to the file or it content) from the flashars that are passed to the .swf file when it is embedded into an html page. This makes the single .swf file truely reusable and easiest to use, because there's no As3 coding required whatsoever.
I haven't tested this but I believe the "root"-stage would be:
this.parent
In the child swf.
check out this question: as3 access variable from parent swf
Good luck!
Accessing variables or movieclips at main timeline is as following:
AS2:
_root.variable_name;
AS3:
MovieClip(root).variable_name;

Updating Flash text via ActionScript

I want to have a score in the bottom corner of my game. I made a new layer called Score, dragged out a text area, converted it to a symbol and named it Score. Then I set its instance name to Score.
The main class of my flash game is called Main.as. However, I can't seem to access the Score text area I made within the code. I get this error:
1046: Type was not found or was not a compile-time constant: Score.
What did I miss? How can I update the text areas text from within my code?
Edit: Forgot to mention I clicked the "Export for ActionScript" box when I converted it to a symbol. And it's of type MovieClip, if that matters.
When you export a symbol for ActionScript 3, Flash make a class for it with the given name (that is the same name you used both for class definition and instance).
You get this error because you're using the same name for class and instace.
You should check on them and use different names.
I usually name my instances ending with _mc
e.g. In your case you should have Score as the class and name your instance score_mc (or just score with lowercase S.
The layer name makes no difference. You need to set the instance name of your dynamic text field, not the clip containing it. There is also no need to convert it to a MovieClip. Create text field on your stage, make sure its not static, give it an instance name that you can use to access it.
Example, I made my text fields name scoreText_txt
//inside main.as
scoreText_txt.text="new text";
If you want to put your text filed inside a MovieClip you would need to give both the clip and your text field a name. You would then access it using .
MyMovieClip.MyText.text="new text"
Don't export for ActionScript unless you intend to create additional logic for the text field (because it will have its own class) or want to add it to the stage dynamically (via addChild()).
With the instance name being Score, give this a go:
Score.text = "822";
I suggest using lowercase instance names to differentiate from CamelCase used for classes. The above would read to more experienced developers as updating a static property text within a class Score.

Form elements do not appear in output

I have created a Flex project in Flash Builder 4.5.
Next I added fl.controls libraries (and then mx.controls libraries) in the project.
I am adding a screenshot so you can see the setup and the code.
However when I run/debug it, nothing appears in there. Totally white.
I've worked with fl.controls before, I used Flash CS5 to compile the ActionScript project and they worked correctly.
Is there any particular reason why it does not work in Flash Builder?
UPDATE: When I add graphics to the text input, i.e.
ti.graphics.beginFill(0xFF0000);
ti.graphics.drawRect(0, 0, 100, 30);
ti.graphics.endFill();
I do see a red rectangle shape. But still no editable text input box. I tried setting ti.editable = true but no use.
Try this:
Create a new FLA in Flash Professional. Add the TextInput component to your library. Notice that not only does the TextInput class get added to the library, but also a folder of "Component Assets" - skins and such. Flash Professional components are not just code - they are code and graphics.
If you want to use fl.controls.TextInput in Flash Builder, you can publish that FLA you just created with the "export swc" option checked. Include that swc in your Flash Builder project, and you'll be able to instantiate the TextInput in your code. If you want to add other Flash components, add them to the library in the FLA and republish the swc.
I would prefer using Flex itself, it will speed up your development. It's simple and does a lot of work for you.
If you do not want to do this you should give your text some properties like width, height, textFormat or use a css document for the text format. You could also give your text a border ( it should be a property of the TextInput ).
Do not forget to set the text format, otherwise the TextField doesn't know which font to use.
Sample for TextInput ( Bottom of Page )
http://help.adobe.com/de_DE/FlashPlatform/reference/actionscript/3/fl/controls/TextInput.html
Sample for Flex
http://help.adobe.com/de_DE/FlashPlatform/reference/actionscript/3/spark/components/RichEditableText.html
You can try this code:
private var ti:TextField = new TextField();
public function FormText(){
//adds ti possibly underneath 'this': stage.addChild(ti);
//adds ti on top of 'this':
stage.addChildAt(ti,getChildIndex(this));
// makes the TextField editable:
ti.type = TextFieldType.INPUT;
}
This should not only make sure that the TextField exists when the class is instantiated, but also puts the form in front of the class.
But this also assumes that the class has been added to the stage; so it may be better to add it like this:
private var ti:TextField = new TextField();
public function FormText(){
addEventListener(Event.ADDED_TO_STAGE, addedHnd);
}
private function addedHnd(e:Event)
{
removeEventListener(Event.ADDED_TO_STAGE, addedHnd);
//adds ti possibly underneath 'this': stage.addChild(ti);
//adds ti on top of 'this':
stage.addChildAt(ti,getChildIndex(this));
// makes the TextField editable:
ti.type = TextFieldType.INPUT;
}
Please review the TextField docs, too.
Why do you add your control to the stage? You should just add it to your form. i.e. this.addChild(ti)

AS3 access dynamic textfield on stage from a class

I have this dynamic textfield on stage, and I want to access it from my xmlloader class, so it can display text from my xml file. I just can't figure out a way to do it.
I found the answer:
MovieClip(root).thetextfieldonstage.text = "yep";
can call the textfield from class.