dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: "string" to variable

  1. #1

    "string" to variable

    Hello all!

    I'm trying to use a string to determine a specific variable.

    For example.
    Code:
    onClipEvent (load) {
         var1 = "var2";
         var2 = new Object();
         var2.a = "no";
    
    
         _root[var1].a = "yes";
    }
    Is this possible? I really do think it is. But I'm really not sure how to go about doing it. The main reason. I have to save/load hundreds of objects/variables. and in order to determine which ones to load, i wanted to make a variable to tell it which ones to load.

    Basically, so I don't have to do this...
    Code:
    onClipEvent(load) {
         if(var1 == "var2") {
              _root.var2.a = "yes";
         }
         if(var1 == "var3") {
              _root.var3.a = "yes";
         }
         if(var1 == "var4") {
              _root.var4.a = "yes";
         }
    }
    So yea, any help is appreciated,
    Thanks!
    ~Bill
    So many Ideas

    So little Time

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    not sure if i fully understand the question, but from what i do.. do u mean something along these lines?

    Code:
    for(var i:Number=0 ; i< 200; i++){
    	
    	if(var1 == "var"+String(i)){
    		_root["var"+String(i)].a = "yes";
    		
    		}
    	}
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    hmmm, not exactly. All I really need to know is how this...

    _root[var1].a = "yes";

    would compute to this....

    _root.var2.a = "yes";


    I havent been able to do it that way. unless its something else thats makeing it not work... So, does that seem like it should work?

    At first I thought it didnt work because what flash is seeing is this...

    _root."var2".a = "yes";

    Which wouldnt make sense in actionscript. but yea. any thoughts?

    ~Bill
    So many Ideas

    So little Time

  4. #4
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    code:

    var1 = "var2";
    var2 = new Object();
    var2.a = "no";
    _root[var1].a = "yes";
    trace(var2.a);


    Works as expected for me...

    K.

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