A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Load external text file to string

  1. #1
    Senior Member
    Join Date
    Sep 2005
    Location
    Gothenburg, Sweden
    Posts
    357

    Load external text file to string

    Hello

    Is there anyway to add external files to the a string? I have this one:

    header_text = "SOME RANDOM TEXT";

    and i would like to use an external text file instead of "some random text".

    Thanks in advance!
    /xzerox... Take a look at http://www.vmgcomputers.com/h75

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    You can have text in an external file and load it with the LoadVars Class.
    The, you use the onData() method to assign the loaded text to the variable:
    code:

    var header_text:String;
    var my_lv:LoadVars = new LoadVars();
    my_lv.onData = function(src:String)
    {
    if (src != undefined)
    {
    header_text = src;
    trace(header_text);
    }
    else
    {
    trace("An error occured");
    }
    };
    my_lv.load("external.txt");


  3. #3
    Senior Member
    Join Date
    Sep 2005
    Location
    Gothenburg, Sweden
    Posts
    357
    thanks!!!!
    /xzerox... Take a look at http://www.vmgcomputers.com/h75

  4. #4
    Senior Member FlashyGirl's Avatar
    Join Date
    May 2003
    Location
    Portugal
    Posts
    117
    This works very well, but if I use the variable header_text outside the function, it doesn't work and gets "undefined"... I tried putting it as global and all, but nothing worked... what can I do?

  5. #5
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    If you use it after the onData() has been triggered, it will be available from the same timeline.

    For example:
    code:

    this.onMouseDown = function()
    {
    trace (header_text);
    }


  6. #6
    Senior Member FlashyGirl's Avatar
    Join Date
    May 2003
    Location
    Portugal
    Posts
    117
    I don't understand what you mean... I used that code before any other actionscript and on the same action layer, and if I trace header_text it shows undefined.

    You see, I'm loading sounds that are on a folder, and instead of having to open flash and edit an array everytime I add or remove songs, I wanted it to get the number of songs from a text file so that number could be part of a for cycle to add numbers to the array, so it would be dynamically loaded. But as header_text is not recognised outside of that code, I don't know what to do...

  7. #7
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    ah, olá,

    It shows undefined because the data hasn't loaded already. It always takes some time before the data finishes loading and becomes available.

    The onData() is triggered when the data is received, that's the moment when the data becomes available. Before that, you'll always get undefined.
    If you trigger the code you want to trigger inside the onData(), you're making sure the variable is available.
    code:

    var header_text:String;
    var my_lv:LoadVars = new LoadVars();
    my_lv.onData = function(src:String)
    {
    if (src != undefined)
    {
    header_text = src;
    // do what you want here
    }
    else
    {
    trace("An error occured");
    }
    };
    my_lv.load("external.txt");


  8. #8
    Senior Member FlashyGirl's Avatar
    Join Date
    May 2003
    Location
    Portugal
    Posts
    117
    Eu já tinha reparado pelo teu nome que devias de ser Português. Mas vou escrever a dúvida em inglês caso haja alguém com o mesmo problema...


    Well, I already have tried putting all my code inside the if statement as you say there, but when I did that, the songs just didn't play... No sound at all.
    Last edited by FlashyGirl; 05-03-2007 at 06:53 AM.

  9. #9
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003


    Post your code, please.

  10. #10
    Senior Member FlashyGirl's Avatar
    Join Date
    May 2003
    Location
    Portugal
    Posts
    117
    My code is making a random, but no repeat play of songs created by an array.
    The thing is that I wanted that array to pick up from the external txt file the number of mp3 files on the folder.

    Code:
    musica = 1;
    mp3s = new Array(10); //this is the array that must get the number of files on the text file
    for (i=0; i<mp3s.length; i++)
    {
    	mp3s[i] = musica;
    	musica++;
    }
    temp = [];
    function init()
    {
    	temp = mp3s.concat();
    	temp.sort(function ()
    	{
    		return Math.floor(Math.random()*3)-1;
    	});
    	playTracks();
    }
    function playTracks()
    {
    	if (temp.length == 0)
    	{
    		init();
    	}
    	toPlay = temp.pop();
    	myMP3 = new Sound(this);
    	myMP3.loadSound("musica/"+toPlay+".mp3", true);
    	myMP3.setVolume(25)
    	//this is for a fade action I trigger on another frame
    	maxvolume = 25;
    	minvolume = 0;
    	////
    	trace("Música que está a tocar: "+toPlay);
    	myMP3.onSoundComplete = function()
    	{
    		playTracks();
    	};
    }
    init();
    stop();
    I tried this inside the if condition and it didn't work.

  11. #11
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    What about:
    code:

    stop();
    var mp3s = new Array();
    var temp = [];
    function init() {
    temp = mp3s.concat();
    temp.sort(function () {
    return Math.floor(Math.random()*3)-1;
    });
    playTracks();
    }
    function playTracks() {
    if (temp.length == 0) {
    init();
    }
    toPlay = temp.pop();
    myMP3 = new Sound(this);
    myMP3.loadSound("musica/"+toPlay+".mp3", true);
    myMP3.setVolume(25);
    //this is for a fade action I trigger on another frame
    maxvolume = 25;
    minvolume = 0;
    ////
    trace("Música que está a tocar: "+toPlay);
    myMP3.onSoundComplete = function() {
    playTracks();
    };
    }

    var my_lv = new LoadVars();
    my_lv.onData = function(src:String) {
    if (src != undefined) {
    src = parseInt(src, 10); // make sure it's a decimal number
    trace ('src -> ' + src);
    //
    for (i=0; i<src; i++) {
    mp3s[i] = i+1;
    }
    init();
    } else {
    trace("An error occured");
    }
    };
    my_lv.load("external.txt");


  12. #12
    Senior Member FlashyGirl's Avatar
    Join Date
    May 2003
    Location
    Portugal
    Posts
    117
    Muito obrigada! Funciona que é uma maravilha!

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