3 Replies Latest reply on Jun 3, 2009 2:11 PM by vorpal

    setting the name of a new object from a string

      Is there anyway I can set the object name of a newly created object from a string?


      (the code below generates a compile time error on the variable declaration)

      public function addText(newTxt:String, txt:String, format:TextFormat):void {
      var this[newTxt]:TextField = new TextField();
      this[newTxt].autoSize = TextFieldAutoSize.LEFT;
      this[newTxt].background = true;
      this[newTxt].border = true;
      this[newTxt].defaultTextFormat = format;
      this[newTxt].text = txt;

      called using>

      addText("mytxt", "test text", format);

      I could then reference the object later on without using array notation using mytxt.border = false; for example

      There are many a time when I want to set the name of a new object from a string.

      In this example I have a function that adds a new text object to a sprite.

      The problem is, if I call the function more than once then two textfield objects will exist, both with the same name. (either that or the old one will be overwritten).

      I need a way of setting the name of the textfield object from a string.

      var this[newTxt]:TextField = new TextField()

      does not work, If I take the "var" keyword away it thinks it a property of the class not an object.

      resulting in >

      ReferenceError: Error #1056: Cannot create property newTxt on Box.

      There must be a way somehow to declare a variable that has the name that it will take represented in a string.

      Any help would be most welcome