0
votes

I have a movieclip with a dynamic TextField in it, it has the text "Software part" in it by default. In the class of this movieclip I have the following code:

package  {

import flash.display.MovieClip;
import flash.text.TextField;


public class soft_4 extends MovieClip {

    public static var software_part_4_text:TextField;
    public static var software_part_4_text_bg:String;
    public static var software_part_4_text_tu:String;
    public static var software_part_4_text_li:String;
    public static var software_part_4_text_de:String;

    public function soft_4() {
        // constructor code

        software_part_4_text_bg = "Софтуерна част";
        software_part_4_text_de = "Software-Teil";
        software_part_4_text_tu = "Yazılım bölümü";
        software_part_4_text_li = "Programinės įrangos dalis";

        software_part_4_text = software_part_4;
        software_part_4_text.selectable = false;
    }
}

}

I have an instance of this class in my Main class and depending on a button press, the strings in the TextField are changed like so:

soft_4.software_part_4_text.text = soft_4.software_part_4_text_de;

For instance that changes the text in the TextField to be in German. It works for the first instance of this class (for example: public var firstInstance:soft_4 = new soft_4()) that I have on stage BUT the second instance (for example: public var secondInstance:soft_4 = new soft_4()) has the default text which is "Software part".

I've embedded the font that I am using in the text field.

1
where does the variable 'software_part_4' come from? Do you mean software_part_4_text to be a static variable?PeteG
thats the instance name of my textbox that I have added in the movieclip, im sorry I should have clarifiedDimitar Velev
Try removing the static from software_part_4_text. It looks like you mean it to be an instance variable. If it's static it'll be the same between each instance.PeteG
I am accessing the textfield software_part_4_text from my main class so it has to be static, the idea is that once the text changes, it should be the same for every other instance of my soft_4 class.Dimitar Velev

1 Answers

0
votes

You seem confused about the way static variables work. When you set the static variable software_part_4_text, all you are doing is setting a static reference to an instance object. Each time you call new soft_4(); you are making a new instance of that class. And each one of those instances has it's own TextField instance software_part_4. Each instance is unrelated to all the rest.

So this line software_part_4_text = software_part_4 is just setting a static reference to your instance variable. It does not make all of your instances to be the same. When you change the text property of the static reference, you are only updating that single instance. This will NOT have any effect on all the other instances.

What you need to do is update each particular instance separately. If you want to do this with a single call, you pretty much need to store a reference to all of the instances. You could do this with an array and a loop, like so:

public class soft_4 extends MovieClip {

    //Make a static reference to a String, instead of a Textfield
    //This can be private, as it will only be used internally
    private static var software_part_4_text:String;

    //Should these be Constants??
    public static var software_part_4_text_bg:String = "Софтуерна част";
    public static var software_part_4_text_tu:String = "Yazılım bölümü";
    public static var software_part_4_text_li:String = "Programinės įrangos dalis";
    public static var software_part_4_text_de:String = "Software-Teil";

    //This is the Vector used to store all the instances.
    //This can be private, as only this class will ever need to know about it.
    private static var _instances:Vector.<TextField> = new Vector.<TextField>();

    public function soft_4() {
        // constructor code

        software_part_4.selectable = false;

        //Each time a new instance is created, store it in the vector.
        _instances.push(software_part_4);
    }

    //This is the static function you would call from your main timeline or whatever.
    //Pass in the string that you want the new Text to be
    public static function UpdateAllTextFields(newText:String):void
    {
        software_part_4_text = newText;
        _instances.forEach(updateTextField);
    }

    //This is the function that is executed over each instance in the Vector
    private function updateTextField(tf:TextField, index:int, vector:Vector.<TextField>):void {
        tf.text = software_part_4_text;
    }
}

Then from your Main class you could write something like this:

soft_4.UpdateAllTextFields(soft_4.software_part_4_text_de);