AS3 / TextField on movieClip - actionscript-3

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.

Related

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 how can i prevent that a new instance is created by entering a frame?

i am working with several nested movieclip objects in a project. but i get into trouble with the buttons i created and implemented in the nested movieclips:
to describe it in a simple way:
I have a main movieclip with five frames, including two buttons with listeners to browse between the frames. Then inside of one Frame I have another movieclip with its own buttons. i instanciated it by hand not through code and gave it a specific name like "nestedMc".
Now I dont want to build the Listeners for those buttons inside the class of the nested movieclip class but in its parent class, which works fine until i then goto another frame in the main movieclips timeline and come back.
obviously every time flash enters a frame its contents get created anew (and therefore get new instance names). I could now try solve this through filling the frames via code.
But maybe there is another way to make sure the frame contains the same instance everytime i enter?
Timeline scripting is a dirty business, and really, a carry-over compatibility layer for Actionscript 2 projects. Whenever possible, I highly recommend not doing it, and simply keeping all of your code in your document class. As you're experiencing, timeline code causes headaches.
Consider instead just creating both states of your Stage (it sounds like that's what your two buttons are jumping between) and simply hiding them offstage or setting their alpha to zero and their mouseEnabled state to false. Furthermore, if the purpose of your frames is to play animation (a tween), consider instead switching to a much more powerful suite such as TweenLite. Moving an object over a hundred pixels (smoothly) can be as easy as:
TweenLite.to(redBall, 3, {x:100});
Now, if you're manually adding these items to the stage, as long as the object is a dynamic one, you can assign an instance name to it which will be saved between frame loads. Be aware the object name is not the same as the instanced name. For example:
var redBall:Ball = new Ball();
redBall.name = "bubbles";
The object's name is Ball, but it's represented as a variable called redBall. Its actual DisplayList name will likely be ambiguous (such as "Instance71"), and I can manually define it as "bubbles". 3 different names for the same object, all very different and necessary.
Even if you give the object a displayList name, you may not be able to reference it through code unless you enable Automatically declare stage instances, which basically creates on each object a pointer to the displayList object.
That said, you can always fetch the object by other means. Obviously, your buttons are always appearing, but you're trying to find a very specific object on the stage. At this point, we can use getChildByName() or getChildAt().
Hope that helps.
-Cheers

When do we need to add reference to the stage from one class to another

When do we need to pass reference to the stage from one as3 class to another like in this tutorial
http://asgamer.com/2009/as3-flash-games-for-beginners-firing-weapons-with-delays
he added a ref to the stage from the bullet class to the ship class
as I understand a reference is needed when we want to use a function in a certain class from another class but why do we have to reference the stage isn't it only one stage for the whole project or each class has it's own stage ?
I am very confused
Thanks
Only objects that are connected to a stage will be shown on screen. In the tutorial he is adding the lasers onto the stage display list so that it appears on screen. Until it is added, it will not be rendered regardless of the visible property.
See this for more info about the display list.
#Geotarget is correct, but the answer is a little bit indirect.
Objects that are not on the display list do not actually have a reference to stage. So if, for example, you create a var mc:MovieClip = new MovieClip(); which is not added to the display list (as in addChild(mc)), then mc.stage will be null. (Also, checking if(mc.stage){[...]} is also a way to verify if the MovieClip is part of the display list yet.)
So you can pass a reference to the stage to non-display-list objects to allow them access to things like stageWidth.
Yes, a reference is needed when you wanna have to access a function present in that particular class ( to which reference belongs). This is one of the uses.
There is only one stage for the whole project.
In the tutorial you are following, both the classes are using the reference of a COMMON Stage, so that both of them can access the Stage.
It's like giving the address of a place to two people. So that both of them can go there. Naturally, giving addresses doesnot mean, we are building two places for each of them.
V.

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.

Recreating stage layout in as3

I have 27 MovieClips in my Library which are all images. At the moment they are positioned on the stage as instances of their parent and then made to function in the first frame of my actions layer. I want to recreate this layout solely in code so there is nothing on the stage. How do I do this?
Thanks in advance.
Sam
Right click on a movieclip in the library, then go to Properties.
Tick "Export for ActionScript", then check the name where it says "Class". Hit OK.
Let's say this name was "Symbol1".
Then type this script:
var symbol1:MovieClip = new Symbol1();
addChild(symbol1);
var symbol1 means you created a variable, MovieClip is the type. This MovieClip variable is a "new " Symbol1 (this was the name in the library, Properties, Class.
Then add this to the stage:
addChild(symbol1)
If you want to position it on the stage, set the coordinates of the variable:
symbol1.x = 10;
symbol1.y = 10;
puts it to (10, 10).
Depending on how many objects you have you can type this code for each one of them (don't forget to Export them for actionscript in Library->Properties).
If you have tons of movieclips and you don't want to type evertyhing, but would rather write some dynamic code, give us a hint on your library structure and how you named your objects.
Hope this helps.