A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Change dynamic text color

  1. #1

    Change dynamic text color

    So I have this typewriter script that looks like this:
    frame 1:
    Code:
    firstname = "Tom";
    _root.strText = "Hi "+firstname+". Allow me to introduce myself.";
    _root.counter = 1;
    frame 2 (labeled "loop"):
    Code:
    _root.text1 = _root.strText.substr(0,_root.counter);
    _root.counter++
    frame 3:
    Code:
    gotoAndPlay("loop");
    I'm looking to get 'firstname' to be a different color than the rest of the text. How can I do this?

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    What version of flash are you using?

  3. #3
    sorry.. using cs3 with as2.0

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    I'm using MX2004, maybe you can adapt this to want you are trying to do...
    Code:
    // txt1 and txt2 are instances of the same movieclip, txt2 masks txt1
    // font used in dynamic text field needs to be in library and exported
    stop();
    var firstname = "Tom";
    var firstnameHTML = "<FONT COLOR='#FF0000'>" + firstname + "</FONT>";
    //
    txt1.myTxt.htmlText = "Hi " + firstnameHTML + ". Allow me to introduce myself.";
    //
    var myStrText = txt1.myTxt.text;
    var CT = 0;
    txt2.onEnterFrame = function() {
    	txt2.myTxt.htmlText = myStrText.substr(0, CT++);
    	if (CT > myStrText.length) {
    		delete this.onEnterFrame;
    	}
    };
    //
    Last edited by dawsonk; 09-03-2008 at 02:24 PM.

  5. #5
    Thanks dawsonk, but I was looking to have the text be center aligned. I notice when i change this the effect doesn't work that same. I started working with another typewriter script:
    Code:
    var firstname = "Tom";
    var phrase_string:String="Hi "+firstname+". Allow me to introduce myself";
    var n:Number= phrase_string.length;
    var i:Number=0;
    this.onEnterFrame=function(){
     if (i<n){
    //display_txt is the instance of my dynamic text box
      display_txt.text+=phrase_string.substr(i,1);
      i+=1;
     }
    }
    I tried to mash up what you posted
    Code:
    hover.fin.fish.openMov.gotoAndStop(1);
    var firstname = "Tom";
    var firstnameHTML = "<FONT COLOR='#FF0000'>"+firstname+"</FONT>";
    var phrase_string = "Hi "+firstnameHTML+". Allow me to introduce myself.";
    var n:Number = phrase_string.length;
    var i:Number = 0;
    this.onEnterFrame = function() {
    	if (i<n) {
    		//display_txt is the instance of my dynamic text box
    		display_txt.htmlText += phrase_string.substr(i, 1);
    		i += 1;
    	}
    };
    but my script doesn't literally spells out "Hi <FONT...."

  6. #6
    i don't see much of a difference between what you posted and what i posted.. can any one explain this to me?

  7. #7
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    //
    var firstname = "Tom";
    var phrase_string = "Hello " + firstname + ". Allow me to introduce myself.";
    //
    var sX = phrase_string.indexOf(firstname);
    var eX = sX + firstname.length;
    var phraseArray = phrase_string.split("");
    var n:Number = phraseArray.length;
    for (j = 0; j < n; j++) {
    	if (j >= sX && j < eX) {
    		phraseArray[j] = "<font color='#FF0000'>" + phraseArray[j] + "</font>";
    	}
    }
    //
    var phrase = "";
    var i:Number = 0;
    this.onEnterFrame = function() {
    	if (i < n) {
    		phrase += phraseArray[i];
    		//display_txt is the instance of my dynamic text box
    		this.display_txt.htmlText = "<p align='center'>" + phrase + "</p>";
    		i++;
    	} else {
    		delete this.onEnterFrame;
    	}
    };

  8. #8
    beautiful dawsonk! thanks a lot!

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