A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Can't access variable from within movieclip.

  1. #1
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520

    Can't access variable from within movieclip.

    On the first frame I have a string variable called "nextSection". When the user clicks on a menu item on the site then "nextSection" is populated with the name of that section.

    Later on in the fla I have a movieclip containing various sections for the site. The swf compiles without any problems.

    I'm now attempting to make the file smaller and easier to update. Rather than have 8 different movieclips that load images, I thought I would just have the one. I changed the following lines in my fla
    PHP Code:
    // nested within 2 movie clips (sections.aboutUs)
    loadImage("aboutUs/mainImage.jpg");

    //nested within 2 movie clips (sections.outdoorAdvertising)
    loadImage("outdoorAdvertising/mainImage.jpg");

    // there are 8 of these in total. 
    to
    PHP Code:
    loadImage(nextSection+"/mainImage.jpg"); 
    I'm now getting the error "1120: access of undefined property nextSection". The variable does not contain anything when the file compiles. Only when the user clicks on a menu item does the variable get filled (ie: if user clicks on the OutDoor Advertising button, the variable contains the string "outdoorAdvertising" which is also a folder in the site directory). I tried to populate it with the name of one of the sections on the first frame ( ie: nextSection:String="aboutUs"; ) just to see if it worked but still got the error.
    Last edited by CVO Chris; 09-15-2010 at 10:10 AM.

  2. #2
    Senior Member
    Join Date
    Jan 2009
    Posts
    208
    What does this function loadImage do?

    You are loading these images externally?

  3. #3
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    Yeah images are being loaded externally.

    I don't know if this helps but the function is as follows:
    PHP Code:
    function loadImage(url:String):void {
        
    imageLoader = new Loader();
        
    imageLoader.load(new URLRequest(url));
        
    imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESSimageLoading);
        
    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETEimageLoaded);


  4. #4
    Senior Member
    Join Date
    Jan 2009
    Posts
    208
    So why do you have this fucntion:

    loadImage(nextSection+"/mainImage.jpg");

    you should put the url from where the image resides, for example:

    loadImage(imagesFolder/mainImage.jpg");

    and then in imageLoaded, you get the image (loader) and put it where oyu want to.

  5. #5
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    That's what I originally had but there are 8 instances of this in my timeline. I'm trying to pass a variable in to the argument of the loadImage function so that instead of having 8 instances of the code I could have 1. Then if I need to change something, instead of changing all 8 instances I just have to update the one.

    Basically, is it possible to add a variable in a function's argument? ie:

    FUNCTION (VARIABLE+"whatever");

    If this is possible then why am I receiving an error? The variable I'm using is declared on the main timeline and the function is called within a nested movieclip off the main timeline.

  6. #6
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    can you trace nextSection at all? If it is set on the main timeline and you are trying to access from within a child movieclip you may have to look for stage.nextSection

  7. #7
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    I've tried the following:
    trace (stage.nextSection)
    trace (parent.parent.nextSection)
    trace (root.nextSection)

    Still getting the error.

    More information which just came to mind: although the nextSection variable is declared on the main timeline, it is only given a value inside a function (which is also on the main timeline). I think I remember something about if a variable is created within a function then only that function has access to it. Would it also be the same if a function added a value to a variable? (ie: is that the same thing as the function creating the var which is why I can't access it?) Or am I barking up the wrong tree with this?
    Last edited by CVO Chris; 09-15-2010 at 11:05 AM.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You can probably access it through the root, but you'll have to cast root to something which can have a nextSection property. The main timeline corresponds to the root. Since the root property is typed as DisplayObject (see livedocs), you have to cast it to MovieClip or whatever your document class is to access properties and methods that DisplayObject doesn't have.

    Code:
    MovieClip(root).nextSection
    MovieClip of course does not declare nextSection either, but it is dynamic so it can have arbitrary properties.

    Setting a variable value within a function does NOT restrict that value to the scope of the function. However, if you redeclare a variable inside a function, you are not using the outer one because you've overridden it in the function scope. Make sure you do not have "var nextSection" in your value setting function.

  9. #9
    Juvenile Delinquent CVO Chris's Avatar
    Join Date
    Jul 2002
    Location
    Ulster, UK
    Posts
    520
    loadImage(MovieClip(root).nextSection+"/mainImage.jpg");

    Works a charm.

    Thanks 5Tof (again!)

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