A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Loading arrays from a text file into a movie

Hybrid View

  1. #1

    Loading arrays from a text file into a movie

    I have created an mp3 player, in MX 2004, that uses arrays to dynamically populate the movie clip. What I would like to do now is place the 2 arrays, artist and title info into a text file, so I can use just one movie to play various cds. Unfortunately, I have not found a good tutorial on how to accomplish this.

    It is a basic script that I will incorporate into the mp3 player once I get the script to work right. There are links in the main movie that will send the appropriate text file to the player.

    Anybody have any suggestions?

    THe following code is what I want placed in a text file:

    -----------------------------------------------------
    var titles = new Array("Track01","Track02","Track03","Track04","Tra ck05","Track06");
    var songNames = new Array("Track01.mp3","Track02.mp3","Track03.mp3","T rack04.mp3","Track05.mp3","Track06.mp3");
    var artist = "Jeffrey Smith";
    var cd = "Let Me Worship You";
    -----------------------------------------------------

    The following code will be located in the movieclip:

    ----------------------------------------------------
    artist_txt.text = artist;
    cd_txt.text = cd;
    var sectionCount = titles.length;

    var menu = this.createEmptyMovieClip("menu_mc", 1);
    menu._x = 25;
    menu._y = 70;


    for(var i=0; i < sectionCount; i++){
    var depthCount = menu.getNextHighestDepth();
    var item = menu.attachMovie("itemClip", "item_" + i, depthCount);
    item.labelName = titles[i];
    item.song = songNames[i];
    item.targetClip = this;
    item._y = i*45;
    }
    ----------------------------------------------------

    Attached is the test fla.

    Any help would be greatly appreciated,
    Rick
    Attached Files Attached Files

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Use a comma delimited sting in your text file, and use the String.split() function in Flash to parse the string into an array:

    In your text file:

    titles=Track01,Track02,Track03,Track04,Track05,Tra ck06&songNames=Track01.mp3,Track02.mp3,Track03.mp3 ,Track04.mp3,Track05.mp3,Track06.mp3

    In Flash:
    code:

    var lv=new LoadVars();

    lv.onLoad=function(ok){
    if(ok){
    titlesArray=this.titles.split(",");
    songs=this.songNames.split(",");
    }
    else{
    trace("text file failed to load"):
    }
    }

    lv.load("myTextFile.txt");



    split:
    http://www.macromedia.com/livedocs/f...html#wp4005922

    K.

  3. #3

    How do I join the create movie loop with the array?

    Thanks for replying to my thread, I am really at the end of my rope on this one.

    I created the following songs.text file:

    &artist=Jeffrey Smith&cd=Let Me Worship You
    &titles=Track01,Track02,Track03,Track04,Track05,Tr ack06&songNames=Track01.mp3,Track02.mp3,Track03.mp 3,Track04.mp3,Track05.mp3,Track06.mp3

    loaded the code below into my flash movie. I can't get the data to display. Something is wrong with my loop. I traced both arrays and they show up in a straight line instead of descending in the movie clip. Nothing I do can get them to display like they do in the original menuarray_100.fla

    ---------------------------------------
    var lv=new LoadVars();

    lv.onLoad=function(ok){

    if(ok){

    titles=this.titles.split(",");

    songs=this.songNames.split(",");

    artist_txt.text = this.artist;
    cd_txt.text = this.cd;

    var sectionCount = titles.length;

    var menu = this.createEmptyMovieClip("menu_mc", 1);
    menu._x = 25;
    menu._y = 70;


    for(var i=0; i < sectionCount; i++){
    var depthCount = menu.getNextHighestDepth();
    var item = menu.attachMovie("itemClip", "item_" + i, depthCount);
    item.labelName = titles[i];
    item.song = songNames[i];
    item.targetClip = this;
    item._y = i*45;
    }
    }

    else{

    trace("text file failed to load");

    }

    }



    lv.load("songs.txt");

  4. #4

    2 days countless hours and still no results

    All I want to do is populate an array, that is inside a flash movie, with data from a text file. The array in the flash movie creates a seperate movie for each mp3 file.

    The following code populates the array from inside the movie and works well. I would like the data to come form a text file.

    Code:
    var titles = new Array("Track01","Track02","Track03","Track04","Track05","Track06");
    var songNames = new Array("Track01.mp3","Track02.mp3","Track03.mp3","Track04.mp3","Track05.mp3","Track06.mp3");
    var artist = "Jeffrey Smith";
    var cd = "Let Me Worship You";
    
    artist_txt.text = artist;
    cd_txt.text = cd;
    var sectionCount = titles.length;
    
    var menu = this.createEmptyMovieClip("menu_mc", 1);
    menu._x = 25;
    menu._y = 70;
    
    	
    for(var i=0; i < sectionCount; i++){
    	var depthCount = menu.getNextHighestDepth();
    	var item = menu.attachMovie("itemClip", "item_" + i, depthCount);
    	item.labelName = titles[i];
    	item.song = songNames[i];
    	item.targetClip = this;
    	item._y = i*45;
    }
    I would like to move the following code into a text file:

    Code:
    var titles = new Array("Track01","Track02","Track03","Track04","Track05","Track06");
    var songNames = new Array("Track01.mp3","Track02.mp3","Track03.mp3","Track04.mp3","Track05.mp3","Track06.mp3");
    var artist = "Jeffrey Smith";
    var cd = "Let Me Worship You";

    I have tried the following:

    Code:
    var lv=new LoadVars();
    
    
    
    lv.onLoad=function(ok){
    
    if(ok){
    
    titlesArray=this.titles.split(",");
    
    songs=this.songNames.split(",");
    
    }
    
    else{
    
    trace("text file failed to load"):
    
    }
    
    }
    
    
    
    lv.load("songs.txt");
    But I cannot get the data to populate my movie clips. I have spent two days on this and still have gotten no where. Attached is the text file I am working with. I have even included a screen shot of the successful movie. Can anybody help?
    Attached Images Attached Images
    Attached Files Attached Files

  5. #5
    Senior Member
    Join Date
    Jul 2001
    Location
    Planet Earth
    Posts
    298
    There are two changes to make your code:

    1. Replace
    Code:
    var menu = this.createEmptyMovieClip("menu_mc", 1);
    with
    Code:
    var menu = _root.createEmptyMovieClip("menu_mc", 1);
    Using this inside of a function meant you were creating the empty inside the function (invisible to the eye) instead on the main time where the user can see it

    2. in your for loop replace
    Code:
    item.song = songNames[i];
    with
    Code:
     item.song = songs[i];
    The array of songs you create is called songs not SongNames
    Attached Files Attached Files
    ---
    Thinking outside of the box will get you fired if the "box" is strict budget.

  6. #6
    THANK YOU, THANK YOU, THANK YOU!!!

    Life is complete,
    Rick

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