A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [MX04] Loaded variable doesn't work with array

  1. #1
    Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    60

    [MX04] Loaded variable doesn't work with array

    Hi All,

    First frame:
    Loaded external variables from txt file. "movies" is the variable I need to work with the arrays in the last frame. "data" is the variable that changes to 1 when the variables are loaded.
    PHP Code:
    done=0;
    movies=0;
    loadVariables("data2.txt",_root); 
    Second frame: checks if load is complete:
    PHP Code:
    if(done==1){
        
    gotoAndPlay(4);

    Third frame: loops:
    PHP Code:
    gotoAndPlay(2); 
    Last Frame: has arrays on it:
    PHP Code:
    //new array
    arr1 = []; 
    //create entries with number of entries as required
    for (var n=0n!=moviesn++) arr1.push(n); 

    //new array
    arr1b = []; 
    //create entries with content from arr1 +".swf"to string
    for (var b=0b!=moviesb++) arr1b.push(arr1[b]+".swf"); 

    stop(); 
    Now I know that the variables successfully load, and if I declare the variable "movies" as a number instead of loading it the arrays work also.

    So why can't I get the loaded variable to work in the last frame?
    HELP PLEASE!
    In skating over thin ice, our
    safety is in our speed.

  2. #2
    I Love India Nalini_v's Avatar
    Join Date
    Jun 2007
    Location
    Chennai
    Posts
    195
    My suggestion is use the your array variable as "global" type.

  3. #3
    Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    60
    Sorry, I'm a bit slow. Can you clarify that?
    In skating over thin ice, our
    safety is in our speed.

  4. #4
    I Love India Nalini_v's Avatar
    Join Date
    Jun 2007
    Location
    Chennai
    Posts
    195
    First declare the array as the _global variable and use the code below in the last frame in flash

    //new array
    _global.arr1 = new Array();
    //create entries with number of entries as required
    for (var n=0; n!=movies; n++) _global.arr1.push(n);

    //new array
    _global.arr1b = new Array();
    //create entries with content from arr1 +".swf"to string
    for (var b=0; b!=movies; b++) _global.arr1b.push(_global.arr1[b]+".swf");

    stop();

  5. #5
    Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    60
    Hi Nalini,

    It ain't working, I'm afraid. Is there any way to "extract" a number from a variable; or speciy it as a number? I think the array is having a problem with this because its seing a string not a number.
    In skating over thin ice, our
    safety is in our speed.

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    loadVariables is "old school" method
    use a LoadVars object and its onLoad function
    PHP Code:
    arr1 = []; arr1b = []; 

    lv = new LoadVars();
    lv.load("movies.txt"); // movies.txt - &movies=5&

    lv.onLoad = function(){
    // all ext. vars are received as "strings", 
    // parseInt converts a string to an integer.
    movies parseInt(lv.movies);
    for (var 
    n=0n!=moviesn++) {
    arr1.push(n); 
    arr1b.push(arr1[n]+".swf");
    trace("arr1b["+n+"] = "+arr1b[n]);

    };

    /*--List Variables--
    Variable _level0.arr1 = [object #1, class 'Array'] [
        0:0,
        1:1,
        2:2,
        3:3,
        4:4
      ]
    Variable _level0.arr1b = [object #2, class 'Array'] [
        0:"0.swf",
        1:"1.swf",
        2:"2.swf",
        3:"3.swf",
        4:"4.swf"
      ]
    */ 
    hth

  7. #7
    Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    60
    Yupp. Parsing ineger code was what I was after. Although your solution to the problem WAS infinitely more elegant. Thanks again. If you ever come to Melbourne contact me and I'll buy you a beer (or twenty.)
    In skating over thin ice, our
    safety is in our speed.

  8. #8
    Member
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    60
    I know I'm probably pushing my luck... but the code I have now works exactly how its supposed to. Except I can't seem to retrieve the contents of the array except through trace(). Is this because I'm addressing the array incorrectly?
    PHP Code:
    arr1 = []; arr1b = []; arr2 = []; arr2b = [];

    lv = new LoadVars(); 
    lv.load("data.txt"); // movies.txt - &movies=5& 

    lv.onLoad = function(){ 
    // all ext. vars are received as "strings", 
    // parseInt converts a string to an integer. 
    movies parseInt(lv.movies); 
    for (var 
    n=0n!=moviesn++) { 
    arr1.push(n); 
    trace("before sort - "+arr1); 
    arr1b.push(arr1[n]+".swf"); 
    trace("arr1b["+n+"] = "+arr1b[n]); 
    arr2.push(n); 

    arr2.sort(function(){return Math.floor(Math.random()*3)-1}); 
    trace("after sort - "+arr2); 
    for (var 
    n=0n!=moviesn++) { 
    arr2b.push(arr2[n]+".swf"); 
    trace("arr2b["+n+"] = "+arr2b[n]);
    }
    };
    stop(); 
    You wrote:
    /*--List Variables--
    Variable _level0.arr1 = [object #1, class 'Array'] [
    0:0,
    1:1,
    2:2,
    3:3,
    4:4
    ]
    Variable _level0.arr1b = [object #2, class 'Array'] [
    0:"0.swf",
    1:"1.swf",
    2:"2.swf",
    3:"3.swf",
    4:"4.swf"
    ]
    */
    And I'm sure the answer is in there, but I don't know how to work with classes. Can I please ask for your help this one last time? I swear if I run into any more problems after this I'll just immolate myself silently.
    In skating over thin ice, our
    safety is in our speed.

  9. #9
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    are you trying to load a random movie .. 0.swf to 4.swf ?
    if so, simplify the whole thing with -
    PHP Code:
    arr = [];  

    lv = new LoadVars();
    lv.load("movies.txt"); // movies.txt - &movies=5&

    lv.onLoad = function(){
    movies parseInt(lv.movies);
    for (var 
    n=0n!=moviesn++) arr.push(n+".swf"); 
    arr.sort(function(){return Math.floor(Math.random()*3)-1}); 
    toLoad arr[0];  trace(toLoad);
    container.loadMovie(toLoad);
    }; 
    hth ??

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