A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: AS3, How to show just the last line of dinamic text (and more)?

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    6

    AS3, How to show just the last line of dinamic text (and more)?

    Hi,

    I have made this simple script that loads log messages from an external text file:

    Code:
    var myTextLoader:URLLoader = new URLLoader();
    myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
    
    function onLoaded(e:Event):void {
    track_info.text = e.target.data;
    
    addChild(track_info);
      addEventListener(Event.ENTER_FRAME, scrollField);
    }
    
    function scrollField(e:Event):void {
      if(track_info.scrollV < track_info.maxScrollV) {
        track_info.scrollV++;
      }else{
        removeEventListener(Event.ENTER_FRAME, scrollField);
      }
    }
    
    myTextLoader.load(new URLRequest("tracks.txt"));

    The log file is tracks.txt and has this structure (it can´t be changed because it is autogenerated by a software that gets the mp3 tags from a player in realtime):

    Code:
    [14-07-2010 20:21:33] Log file created for client.
    [14-07-2010 20:21:33] Client connected.
    [14-07-2010 20:26:21] * Artist 21 - Song 11
    [14-07-2010 20:40:02] * Artist 42 - Song 02
    [14-07-2010 20:45:04] * Artist 14 - Song 10
    [14-07-2010 20:47:19] * Artist 46 - Song 04
    [14-07-2010 20:51:09] * Artist 07 - Song 09
    [14-07-2010 20:54:13] * Artist 54 - Song 01
    [14-07-2010 20:57:32] * Artist 19 - Song 12
    [14-07-2010 21:00:51] * Artist 35 - Song 06
    [14-07-2010 21:04:02] * Artist 43 - Song 08

    That actionscript allows me to show the last played songs in the flash movie, as I expected. But the issue is that:

    1. How can I do to show only the last song (the last line in the text file), which is currently playing?
    2. Is there any way to autoupdate the movie to show the current data from the .txt file? I mean, when a new song is playing, the .txt file receives its info into a new line at the bottom (again, the last line is the now playing song).
    3. Finally, I would like to remove the "[Day-Month-Year Hour:Min:Sec] *" section of each line (the first 25 characters) and show just the "Artist - Song" part. How can it be done?


    Thanks in advance.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    1. Regexp.
    2. No, but you can poll at 1 second intervals or so.
    3. Regexp again.

    More details coming in a little while, should you need them.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    		private var r:RegExp = /^.*\* (.*)/;
    		private var data:String = "[14-07-2010 20:21:33] Log file created for client.\n\
    [14-07-2010 20:21:33] Client connected.\n\
    [14-07-2010 20:26:21] * Artist 21 - Song 11\n\
    [14-07-2010 20:40:02] * Artist 42 - Song 02\n\
    [14-07-2010 20:45:04] * Artist 14 - Song 10\n\
    [14-07-2010 20:47:19] * Artist 46 - Song 04\n\
    [14-07-2010 20:51:09] * Artist 07 - Song 09\n\
    [14-07-2010 20:54:13] * Artist 54 - Song 01\n\
    [14-07-2010 20:57:32] * Artist 19 - Song 12\n\
    [14-07-2010 21:00:51] * Artist 35 - Song 06\n\
    [14-07-2010 21:04:02] * Artist 43 - Song 08";
    Code:
    			trace("data: '" + data + "'");
    			var lastLine:String = data.substring(data.lastIndexOf("\n")+1);
    			trace("lastLine: '" + lastLine + "'");
    			var strippedbit:String = lastLine.replace(r, "$1");
    			trace("final: '" + strippedbit+"'");
    Output:
    data: '[14-07-2010 20:21:33] Log file created for client.
    [14-07-2010 20:21:33] Client connected.
    [14-07-2010 20:26:21] * Artist 21 - Song 11
    [14-07-2010 20:40:02] * Artist 42 - Song 02
    [14-07-2010 20:45:04] * Artist 14 - Song 10
    [14-07-2010 20:47:19] * Artist 46 - Song 04
    [14-07-2010 20:51:09] * Artist 07 - Song 09
    [14-07-2010 20:54:13] * Artist 54 - Song 01
    [14-07-2010 20:57:32] * Artist 19 - Song 12
    [14-07-2010 21:00:51] * Artist 35 - Song 06
    [14-07-2010 21:04:02] * Artist 43 - Song 08'
    lastLine: '[14-07-2010 21:04:02] * Artist 43 - Song 08'
    final: 'Artist 43 - Song 08'
    Note that there is no trailing newline on the last line. If your log does have a trailing newline, you will have to alter that part.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Hi 5Tons,

    Thank you so much for your answers.

    I don´t understand very well your solution, sorry, because I can´t modify the structure of the tracks.txt, it is an automatically generated file.

    Thanks again.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Nothing about the code posted above requires you changing the automatically generated file. My note was simply that if the file has a trailing newline, that you will have to adjust the regexp which gets the last line. Otherwise, it's fine.

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Ok 5Tons,

    I will post the final code here,

    Thanks.
    Last edited by pibo; 07-29-2010 at 08:20 PM.

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Hi again 5Tons,

    I have been working on your code, but without result. Helped, I have written another code that works. I accept any suggestion, thank you:

    Actionscript Code:
    var reload:Timer = new Timer(5000, 0);
    reload.addEventListener(TimerEvent.TIMER, onTimer);
    function onTimer(event:TimerEvent):void{

    var myTextLoader:URLLoader = new URLLoader();
    myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

    function onLoaded(e:Event):void {

    var lines:Array = e.target.data.split("\n");
    var lastLine:String = lines[lines.length - 1];
    var artistAndSong:String = lastLine.substr(24).split(" - ").join("\n");
    track_info.text = artistAndSong;
    addChild(track_info);

    }

    myTextLoader.load(new URLRequest("tracks.txt"));
       
    }
    reload.start();


    Let me ask you one more question. The software that auto-generates the log files, creates one in each session, so I have in the directory something like this:

    tracks,236.txt
    tracks,235.txt
    tracks,234.txt
    ...

    Could it be opened automatically, the latest track file with actionscript?

    TIA.

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