A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How to do a loop to create many SharedObject

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

    How to do a loop to create many SharedObject


    hello all
    in this swishmax file



    When the button is pressed, the text value is incremented by 1 and the value is saved to SharedObject


    PHP Code:
    onSelfEvent (load) {
      var 
    kofa1:SharedObject SharedObject.getLocal("file1"); 
      var 
    kofa2:SharedObject SharedObject.getLocal("file2");                               
               if(
    kofa1.data.chk ==undefined){
               
    kofa1.data.chk =0   ;                  
       }
       if(
    kofa2.data.chk ==undefined){
               
    kofa2.data.chk =0   ;                  
       }    
    }
    onFrame (1) {
        
    stop();
        
    t1.text =  kofa1.data.chk 
        
    t2.text =  kofa2.data.chk 
       
    kofa1.flush ();
       
    kofa2.flush (); 

    i create only button 1 and button 2

    How to do a loop to create many SharedObject for all buttons easily

    swishmax file : https://app.box.com/s/0xcvfv8ib32fm82kbruuh1sv679hiq1t


  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,365
    Hello, I just removed the script from the buttons & gave the buttons names, btn1, btn2 etc...

    The thing about retrieving the shared object files through the main timeline is by referencing the number for the specific button as: this._name.split("n")[1] which basically gets the number after the letter "n" for name "btn1" it will get 1.
    PHP Code:
    onSelfEvent (load) {
    var 
    kofa1:SharedObject SharedObject.getLocal("file"+i);  
               
    for(var 
    i=1;i<20+1;i++){
    kofa1 SharedObject.getLocal("file"+i);                        
    if(
    _root["t"+i].text=="undefined"){
    _root["t"+i].text="0";
    }    

     
    _root["btn"+i].onPress = function(){                                                             //"btn"+i for button can be referenced i but inside the button program must split after "n" on btn1 to get 1
    if(_root["t"+this._name.split("n")[1]].text=="undefined"){
    _root["t"+this._name.split("n")[1]].text="0";                                                 //this._name.split("n")[1] splits passed n for name "btn1" to be a number referenced from this.
    }    

    _root["t"+this._name.split("n")[1]].text=int(_root["t"+this._name.split("n")[1]].text+1);
    var 
    kofa2:SharedObject SharedObject.getLocal("file"+this._name.split("n")[1]);  
    kofa2.data.chk=int(_root["t"+this._name.split("n")[1]].text);
    kofa2.flush();
    }

    }
    }

    onFrame (1) {
        
    stop();
    for(var 
    i=1;i<20+1;i++){
    kofa1 SharedObject.getLocal("file"+i); 
    _root["t"+i].text =  kofa1.data.chk ;  

    if(
    _root["t"+i].text=="undefined"){
    _root["t"+i].text="0";
    }   
         
    kofa1.flush ();

    }

    download v2

  3. #3
    Senior Member
    Join Date
    May 2016
    Posts
    378
    ERROR: Cannot find 'i'
    Scene_1, line 2: var kofa1:SharedObject = SharedObject.getLocal("file"+i)

    i solved it by add ( this.i )

    PHP Code:
    onSelfEvent (load) {
    var 
    kofa1:SharedObject SharedObject.getLocal("file"+this.i); 
    but all text ="undefined" in the first time

    how to solve this

    thank you very much

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,365
    Alright that should fix the error, I was checking undefined as a string.

    download v3

  5. #5
    Senior Member
    Join Date
    May 2016
    Posts
    378
    You are a golden man

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