A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: how to global variable in function

  1. #1
    Senior Member
    Join Date
    May 2016
    Posts
    451

    how to global variable in function

    hello all

    PHP Code:
    onSelfEvent (load) {      
        
    loadV = new LoadVars();                        
        
    MyArray = new Array();
        
    loadV.load("mytext.txt");  
        
    loadV.onData = function(s) {
            
    s.split('&');              
            for(
    i=1p.lengthi++) {            
                
    MyArray[i]=p[i].split('|'); 
            }
            
    parseFloat(MyArray[1][0]);
            
    _global.=n
    }
        
    trace(_global.n)  // undefined
    // here how can i use (n) variable


    how can i use variable n outside the function

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    PHP Code:
    onSelfEvent (load) {   
        var 
    n:Number=0;//Declare n outside of the function.    = _root.n
        
    loadV = new LoadVars();                        
        
    MyArray = new Array();
        
    loadV.load("mytext.txt");  
        
    loadV.onData = function(s) {
            
    s.split('&');              
            for(
    i=1p.lengthi++) {            
                
    MyArray[i]=p[i].split('|'); 
            }
            
    parseFloat(MyArray[1][0]);
            
    _root.=n
        
    }

     
    trace(_root.n)

    Last edited by AS3.0; 01-21-2023 at 01:40 PM.

  3. #3
    Senior Member
    Join Date
    May 2016
    Posts
    451

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Since n is declared in _root.main it will be accessed as _root.main.n .

    download v2 (swish)

  5. #5
    Senior Member
    Join Date
    May 2016
    Posts
    451



    this is swishmax file and mytext.txt : https://app.box.com/s/j2zqr2f1aonp3pa2dk88x2d9mus8trez

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Line 16 happened before mytext.txt was loaded, you have to trace line 16 after the text file is loaded.

    PHP Code:
    onSelfEvent (load) {   
        var 
    n:Number=0;//Declare n outside of the function.    = _root.n
        
    loadV = new LoadVars();                        
        
    MyArray = new Array();
        
    loadV.load("mytext.txt");  
        
    loadV.onData = function(s) {
            
    s.split('&');              
            for(
    i=1p.lengthi++) {            
                
    MyArray[i]=p[i].split('|'); 
            }
            
    parseFloat(MyArray[1][0]);
            
    _root.main.=n
    trace
    (_root.main.n)// here true value
       
    ready();//Call ready after the loaded text file loop.
    }

    function 
    ready(){//Check _root.main.n after text file is loaded
    trace(_root.main.n
    }


    download v3 (swish)

  7. #7
    Senior Member
    Join Date
    May 2016
    Posts
    451
    i cant use ( _root.main.n ) in another mc

    i want make variable (n) global and i use it in any mc

  8. #8
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Can you send me the file with the mc? You need to put a function inside of the mc for the loader to call.
    Last edited by AS3.0; 01-21-2023 at 04:26 PM.

  9. #9

  10. #10
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Hello,

    Inside of the _root.main scripting section, you can loop through each player to call there internal ready() function:
    PHP Code:
    onSelfEvent (load) {   
        var 
    n:Number=0;//Declare n outside of the function.    = _root.n
        
    loadV = new LoadVars();                        
        
    MyArray = new Array();
        
    loadV.load("mytext.txt");  
        
    loadV.onData = function(s) {
            
    s.split('&');              
            for(
    i=1p.lengthi++) {            
                
    MyArray[i]=p[i].split('|'); 
            }
            
    parseFloat(MyArray[1][0]);
            
    _root.main.=n
            
    for(var i=1;i<3;i++){
    _root.main["player"+i].ready();//Call the internal ready function inside of each movieclip.
    }
    }

    Inside of _root.main.player1 etc... you can store the internal ready function:
    PHP Code:
    onSelfEvent(load){
    h=[];  
    function 
    ready(){// << Call ready() from the _root.main text loader, since this movieclip is ready before the loader has any data.
    for( i=1;_root.main.n;i++){
    h.push(i)    
    }
    h.sort(function (a:intb:int):int { return Math.random() > .5 ? -1; });
    _root.data_field.text+=(h) + "   "
    }

    download v4 (swish)
    Last edited by AS3.0; 01-21-2023 at 05:44 PM.

  11. #11
    Senior Member
    Join Date
    May 2016
    Posts
    451
    thank you very much

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