A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] My god, it's full of variables!

  1. #1
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45

    resolved [RESOLVED] My god, it's full of variables!

    Please help me. I'm new to AS3 and thought I could just wing it. Obviously not. This is ridiculous and I've been at this for an embarrassingly long number of hours and hours.

    I'm just trying to pass a value from a function to a global variable; or a variable on the main timeline; or whatever I call it now.

    Please don't berate me for using a global variable. I understand it is bad, but I'm just trying to finish this project and I don't really care right now.

    Code:
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    
    var categoryArray:Array = new Array();
    var songArray:Array = new Array();
    var mySongsDir:Object = new Object();
    var mySongsDir:String = new String();
    
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest("mySongs.xml"));
    
    function LoadXML(e:Event):void {
    	xmlData = new XML(e.target.data);
    	ParseSongs(xmlData);
    }
    //
    function ParseSongs(songInput:XML):void {
    	//
    	mySongsDir = songInput.attribute("filedir").toString();
    
    // ... blah blah blah... rest of function...
    }
    I need to edit the values of categoryArray and songArray from within the function. I also need to edit the value of mySongsDir. I would pass the arrays to the function, or return my values as an object, but I don't think I can!

    _root - No!
    _global - No!

    This is such a simple task... I just want to pass values to the variables in _root. *sob*
    How do I do it?

  2. #2
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Whatever class this is in -- whether it's the document class (i.e. on the main timeline) or something else, if you define the variables at the top, not in a function, then those variables are in the scope of the whole class. So you should be able to just call categoryArray and songArray without anything in front of them and it should just work.

  3. #3
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    I have no class my good man.

    This is just sitting in the actions panel in a frame on the main timeline. I know, it's a scandal!

    And it doesn't work... if I say:

    categoryArray.push("fish","donkey","mongoose");

    It will add it to a virtual array inside the function. When that function is gone, it disappears.

    (That is, unless I am an idiot and my trace statement for the array is executing before the XML is parsed. I was having trouble getting the debugger to tell me what the heck was going on.)

    edit: -- hmmm, interesting thought
    I made the array in the main timeline, but I am making it multidimensional inside a function during the parsing process -- during a for... each statement:

    (outside the function)
    var categoryArray:Array = new Array();

    (inside the function)
    categoryArray[n] = new Array();


    It hurts.
    Last edited by KindaGamey; 09-06-2007 at 08:01 PM.

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    when you create it inside the function, you're creating something within the scope of the function itself. When the parser looks at categoryArray, it first tries to see whether such a variable exists locally inside the function. If not, it will search up in the scope chain. Believe it or not, you DO have class; a class, at least; anything placed on the timeline belongs to an automatically generated "Document Class" which has the same name as the .swf; it extends MovieClip and is a dynamic class that imports basically everything under the sun.
    Now if you want to talk from the function about the outer categoryArray, and you have already defined something within the function called categoryArray which is different from the higher-level "global" categoryArray, you could also reference the outer one by using either:
    this.categoryArray
    or
    root.categoryArray
    Those both refer to the thing you defined at the top of your script.

    edit: I just got what you meant about defining multidimensional sub-arrays, that should not be a problem if you're referring to it directly. It may in fact be true that your XML isn't getting there in time for the trace, that's a whole separate problem; but if you have defined the array at all, it should come up as something, even null, but not throw an error... try root. or this. ...hope that helps...
    Last edited by joshstrike; 09-06-2007 at 08:26 PM.

  5. #5
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    If I try and alter the value of mySongsDir from inside the function then I get this error:

    1151: A conflict exists with definition mySongsDir in namespace internal.

    Outside the function (main timeline):
    var mySongsDir:String = new String();

    Inside the function
    mySongsDir = songInput.attribute("filedir").toString();
    -OR-
    root.mySongsDir = songInput.attribute("filedir").toString();

    Either one will give me the same error.

    Help!
    (I just found the Actionscript 3.0 publishing settings -- I turned off strict and automatic declaration. No effect.)

  6. #6
    Member
    Join Date
    Sep 2007
    Location
    Richmond, VA
    Posts
    45
    Oh my lord. I was declaring it twice.

    PROBLEM SOLVED!

    Thank you for telling me that I can use "root." (bye _root!), but you were right -- I don't even need that. It is all in the same scope.

    *shakes head*

    Programming was so much easier when I smoked cigarettes and could just walk away for a while.

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