SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change font color by timer using actionscript

    hello all,

    I have a client that would like to have the site text change color every 5 seconds - the text buttons included. There is only 5 colors they want to cycle through.

    I would like to be able to set up an actionscript to handle this so I don't have to animate each text.

    Any ideas how I might do this??

    Thanks

  2. #2
    SitePoint Guru krityx's Avatar
    Join Date
    Jan 2006
    Location
    Around
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Actionscript:
     var i:Number = 0;
     function changeColor()
     {
        if (i==0) {
            i++;
            tf.textColor = 0xFF0000;
        }else if(i==1){
            i++;
             tf.textColor = 0xFF0000;
        }else if(i==2){
            i++;
            tf.textColor = 0x552288;
        }else if(i==3){
            i++;
            tf.textColor = 0x7865f2;
        }else if(i==4){
            i=0;
            tf.textColor = 0x162ff6;
        }
     }
    setInterval(changeColor, 5000);


  3. #3
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    krityx,

    What does "tf" refer to?

    Do I need to put a call to changeColor() on the actions frame?

    Is your method compatible with as3 or as2 ??

    Thanks,

  4. #4
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok so I realize that tf is a variable that I have to create for my text fields like:
    Code:
    var tf:TextField = new TextField();
    but how do I get this to apply to all my text feilds already created?

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samohtwerdna View Post
    ok so I realize that tf is a variable that I have to create for my text fields like:
    Code:
    var tf:TextField = new TextField();
    but how do I get this to apply to all my text feilds already created?
    Hi.
    You must create a Dynamic Text input
    named it (in this case) tf then in an other
    layer named ie actions put the code
    posted by krityx.

    Bye.

  6. #6
    SitePoint Guru krityx's Avatar
    Join Date
    Jan 2006
    Location
    Around
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to apply it to all the textfields the easiest way would be to name each of them sequentially (eg : tf1,tf2,tf3) and then use a for loop .

    Code Actionscript:
    var i:Number = 0;
    var n:Number = 3; //number of textfields
     function changeColor()
     {
        for(var j:uint=1; j<=n; j++){
        if (i==0) {
            i++;
            this["tf"+j].textColor = 0xFF0000;
        }else if(i==1){
            i++;
             this["tf"+j].textColor = 0xFF2233;
        }else if(i==2){
            i++;
            this["tf"+j].textColor = 0x552288;
        }else if(i==3){
            i++;
            this["tf"+j].textColor = 0x7865f2;
        }else if(i==4){
            i=0;
            this["tf"+j].textColor = 0x162ff6;
        }
        }
     }
    setInterval(changeColor, 5000);


  7. #7
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by krityx View Post
    to apply it to all the textfields the easiest way would be to name each of them sequentially (eg : tf1,tf2,tf3) and then use a for loop .

    Code Actionscript:
    var i:Number = 0;
    var n:Number = 3; //number of textfields
     function changeColor()
     {
        for(var j:uint=1; j<=n; j++){
        if (i==0) {
            i++;
            this["tf"+j].textColor = 0xFF0000;
        }else if(i==1){
            i++;
             this["tf"+j].textColor = 0xFF2233;
        }else if(i==2){
            i++;
            this["tf"+j].textColor = 0x552288;
        }else if(i==3){
            i++;
            this["tf"+j].textColor = 0x7865f2;
        }else if(i==4){
            i=0;
            this["tf"+j].textColor = 0x162ff6;
        }
        }
     }
    setInterval(changeColor, 5000);
    Hi krityx.
    Sorry to disturb you,
    I'm making my first step
    in AS
    Why does this script work only
    with __root
    PHP Code:
    function circleX(distance:Numberangle:Number):Number {
        return 
    distance*Math.sin(Math.PI*angle/6);
    }
    function 
    circleY(distance:Numberangle:Number):Number {
        return -
    distance*Math.cos(Math.PI*angle/6);
    }
    var 
    center:Number 200;
    for (var 
    i:Number 1i<13i++) {
        
    duplicateMovieClip(number_txt"number"+i+"_txt"i);
        
    // position text field
        
    trace(_root["number"+i+"_txt"]);
        
    _root["number"+i+"_txt"]._x center+circleX(200i);
        
    _root["number"+i+"_txt"]._y center+circleY(200i);
        
    // change text in text field
        
    _root["number"+i+"_txt"].text i;
    }
    var 
    i:Number 0;
    var 
    n:Number 13//number of textfields
     
    function changeColor()
     {
        for(var 
    j:Number=1j<=nj++){
        if (
    i==0) {
            
    i++;
           
    _root["number"+j+"_txt"].textColor 0xFF0000;
        }else if(
    i==1){
            
    i++;
             
    _root["number"+j+"_txt"].textColor 0xFF2233;
        }else if(
    i==2){
            
    i++;
            
    _root["number"+j+"_txt"].textColor 0x552288;
        }else if(
    i==3){
            
    i++;
            
    _root["number"+j+"_txt"].textColor 0x7865f2;
        }else if(
    i==4){
            
    i=0;
            
    _root["number"+j+"_txt"].textColor 0x162ff6;
        }
        }
     }
    setInterval(changeColor500);
    /*
    setInterval(changeColor, 1000);
    trace(circleX(100, 1));
    trace(circleX(100, 2));
    trace(circleX(100, 3));
    */ 
    code by http://www.friendsofed.com/book.html?isbn=1590596188

    Bye.

  8. #8
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help guys,

    what about making a smooth transition between the colors instead of the abrput??

    I assume the Tween class - but where would I implement it??

  9. #9
    SitePoint Zealot samohtwerdna's Avatar
    Join Date
    Jul 2007
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok,

    I added this to help the transition
    PHP Code:
        var final_color:Array = new Array();
        
    final_color[0] = 0x999999//gray
        
    final_color[1] = 0xFF0000//red
        
    final_color[2] = 0x0066CC//blue
        
    final_color[3] = 0xFF66CC//pink
        
    final_color[4] = 0x00CC66//green
        
    final_color[5] = 0x999999//gray
        
        
    alphaOver.addEventListener(TweenEvent.MOTION_CHANGE,tweenToFinal);
        function 
    tweenToFinal(event:TweenEvent):void
        
    {
            
    colorInfo.color Color.interpolateColor(final_color[c],final_color[c+1],event.position);
            
    ex_txt.transform.colorTransform=colorInfo;

        } 
    but now I get a flicker at the end of the interval

    what can I do better to make the whole transition smooth??

  10. #10
    SitePoint Guru krityx's Avatar
    Join Date
    Jan 2006
    Location
    Around
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whisher : maybe the reason it only works with _root is because the textfields are on level0 (or _root) . this[] assumes that the textfields are in the same movieclip or level you're typing your AS on .



Bookmarks

Posting Permissions

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