A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: AS3 Increasing the length of a string

  1. #1
    Member
    Join Date
    May 2005
    Posts
    95

    AS3 Increasing the length of a string

    I am working on the following code and I keep getting a cut off at around the 'C' of Dave's computer.

    I know this is going to be a simple answer but I for the life of me cannot figure out HOW to extend the string visual field. I am pretty sure the secret lies in the variable len.

    Any help would be great.

    Code:
    import flash.text.TextField;
    import flash.events.Event;
    
    var txtFld:TextField = new TextField();
    addChild(txtFld);
    
    txtFld.textColor = 0xFF0000;
    
    var str:String = "Hello, I am Dave's Computer. Even I can tell that TingTing is the most wonderful woman ever.";
    var len:int = str.length;
    var i:int = 0;
    
    this.addEventListener (Event.ENTER_FRAME, onEnter, false, 0,true);
    function onEnter(evt:Event):void {
    	txtFld.appendText (str.charAt (i));
    	i++;
    	if (i > len) {
    		removeEventListener(Event.ENTER_FRAME, onEnter);
    }
    }

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Set the autoSize property.
    Code:
    import flash.text.TextField;
    import flash.events.Event;
    
    var txtFld:TextField = new TextField();
    txtFld.autoSize = TextFieldAutoSize.LEFT;
    addChild(txtFld);
    
    txtFld.textColor = 0xFF0000;
    
    var str:String = "Hello, I am Dave's Computer. Even I can tell that TingTing is the most wonderful woman ever.";
    var len:int = str.length;
    var i:int = 0;
    
    this.addEventListener (Event.ENTER_FRAME, onEnter, false, 0,true);
    function onEnter(evt:Event):void {
    	txtFld.appendText (str.charAt (i));
    	i++;
    	if (i > len) {
    		removeEventListener(Event.ENTER_FRAME, onEnter);
    }
    }

  3. #3
    a little trick that i often use instead of autosize is

    tf.width = tf.textWidth + 10;
    tf.height = tf.textHeight + 10;

    * make sure that you do this after filling the text in the field

  4. #4
    Member
    Join Date
    May 2005
    Posts
    95
    Thank you both for your help!

    While we are on the subject, I am very curious now on how to start a new line.

    So instead of having a sentence like this.

    You
    Could
    Do
    Something
    Like
    This.

    Or even have a sentence,
    like this.

  5. #5
    when adding text? you could setup htmlText and use <br> or you could just use \n in the text

  6. #6
    Member
    Join Date
    May 2005
    Posts
    95
    Ah so it's like JavaScript in that aspect.


    Learn something new every day.

    Thanks much!

  7. #7
    if you are familiar with JS you will find a lot of similarities with actioscript

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center