Updating Flash text via ActionScript - actionscript-3

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.

Related

How to address a text field created within a symbol from main timeline in Flash using actionscript?

How to address a text field created within a symbol from main timeline in Flash using actionscript?
For example, if I have a text field named textfield inside a symbol named symbol1, which is input inside a movie clip called movieclip1, which is, of course, on the main timeline, what could I use to make the textfield half size of the movieclip?
You need to assign a linkage name to the textField in the library first. Let's say you give it a linkage name of title. Now you can do
title.width = title.parent.parent.width/2;

How to access variables within MovieClip (AS3)

I'm new in AS3 and I wanna ask how to access variables inside MovieClips by its name.
in AS2 usually, I use its instance name followed by .(name of variable)
for example...
I have a variable named baru inside a MovieClip named my_mc.
usually I use my_mc.baru to reveal the value of the variable.
Now, i want to know how to do it in AS3
Thank you.
In AS3 there's a few separate but related things you need to be aware of:
All display objects (including MovieClip) can have a name property.
var mc:MovieClip = new MovieClip();
mc.name = "myMC";
myContainer.addChild(mc);
You can find a child by its name using getChildByName() on its parent.
var myMC:MovieClip = myContainer.getChildByName("myMC");
MoveClip instances can have dynamic properties that point to children (or anything at all).
myContainer.myMC = myMC;
(Note that other display object types, such as Sprite, do not allow dynamic properties. Instead you need to create a custom class with class properties.)
When you create a display object in code (as shown above), neither the name or a property on the parent will be automatically created. You can do it manually as shown above.
When you place a symbol instance in the authoring tool and give it an "instance name", both the name and a property on the parent symbol will be assigned by that "instance name". In this case you are not allowed to change the name in code.
Note that in AS2 using createEmptyMovieClip or attachMovie would assign the _name and create a property on the parent, but there's no AS3 equivalent to these functions. This is where a lot of confusion can come in.
So as you can see the way this works is really not different than AS2, except that AS2 had functions that did several things for you (create and add the MovieClip, set the name and add a property on the parent). The problem with AS2 was that you couldn't re-parent an object after it was created, which was a big limitation. In AS3 you can freely move things around.
If you just like to write code inside the MovieClip but not a *.as,you have to add a TextField in the MovieClip by hand,set it's type to Dynamic,then type the variable string in it,then get it use: MovieClip.TextField.text and don't forget: TextField.alpha=0.
It is not a wise way.

AS3: possible to dynamically attach things without linkage id?

Probably a stupid question but..
Is it possible to dynamically add things to the stage using code WITHOUT having manually checked "export for actionscript" for each item, and without having given linkage id's?
I make pages upon pages of "drag and drop" items for people to drag around.. and it just gets unfathomably time consuming to first manually give each item an instance name, then "view in library" for each item one at a time, then modding its Properties, then checking off the box and giving each a linkageID, keeping track of the number etc. Seems like there must be a better/faster way?
In my case, the items are always on the stage to begin with, so I know that they are being exported in the swf, even without having checked off the box...
Oh hey.. since I'm starting with the item in question when the user clicks it, could they instead "duplicate" the item they clicked on? I think this was possible in AS2? Would that still require a linkage id?
Yes, you can just look through the display chain if you know that an item is already on the stage. What you get back will be a DisplayObject and you'll have to decide if it's a MovieClip or a Sprite but it can be done. You can name movie clips on the stage and find them by name, too - these don't need to be exported for scripting.
All DisplayObject instances have a name property and you can use getChildByName to find a named instance at run-time.
You only need to export a symbol for scripting if you want to control the base type of the symbol (so you can add extra properties / methods to it) or if you want to programmatically create new instances of a symbol during run-time. Anything that's on the stage (or inside a display object container on the Stage) can be accessed through an index (not recommended) or through a name and don't need to be exported.
You cannot programmatically create instances of display objects without exporting them first.

AS3 / TextField on movieClip

I have a problem i though i could use this code, but i don't know how to give them a instance name without putting them on the stage.
MovieClipInstanceName.addChild(TextFieldInstanceName);
I do not know if there might be another way to get a TextField to a movieClip. I only work with one frame and with multiple movieClips (pages).
You have a reference to your textfield instance: text1, therefore you can do: mcInstanceName.addChild(text1); You do not need to assign it an instance name if the variable text1 is in the scope.
If you really want to add it an instance name, you can do it like this: text1.name = "myTextField"; Then you can reference this textfield by it, just remember you have to include full or relative path. Example: root.mcInstanceName.myTextField.text = "This is some text"; //this would assume the mcInstanceName is on the main timeline and it cointains your textfield
Instance name (which you define in Flash Professional IDE) is automatically created variable in which pointer to object stored. Also it paste string to object property 'name' with same value.
In your code here is your "instance name"
var text1:TextField = new TextField();
Adding objects as child to other display objects has nothing to do with changing objects instance names except them use them to target this objects.

Updating Flash textbox text via ActionScript

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?