dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] Refer to object on another timeline

  1. #1
    Senior Member
    Join Date
    Nov 2005
    Location
    Milan
    Posts
    119

    resolved [RESOLVED] Refer to object on another timeline

    I have a container movie which loads another movie into a Loader object which then gets added to the main timeline. The loaded movie has a main class which controls all the other classes in that movie. The container movie has one other element - a scrollbar which scrolls the loaded movie (which is longer than the container) up and down.

    When content changes in the loaded movie I need to pass a message to the scrollbar. If I trace parent.parent.parent from the init() function of the loaded movie it traces [object MainTimeline]. My scrollbar, sBar is a direct child of the main timeline, but tracing parent.parent.parent.sBar doesn't work (and anyway it's probably very bad practice!)

    So how can I pass a reference to the scrollbar to the loaded movie? Here's the code in the container movie:

    PHP Code:
    var ldr:Loader = new Loader();
    var 
    sBar:Scrollbar;

    ldr.contentLoaderInfo.addEventListener(Event.INITinitHandler);

    var 
    swf:String "loadedswf.swf";
    var 
    urlReq:URLRequest = new URLRequest(swf);
    ldr.load(urlReq);

    var 
    tempMovieClip:MovieClip = new MovieClip();
    this.addChild(tempMovieClip);
    tempMovieClip.addChild(ldr);

    function 
    initHandler(event:Event):void {
        
    sBar = new Scrollbar(tempMovieClip);
        
    sBar.name "ScrollBarMc";
        
    this.addChild(sBar);


  2. #2
    Member
    Join Date
    Sep 2007
    Posts
    69
    mmmm, consider using custom events instead of referencing nested references parent.parent....

    Whenever the content of the movie changes, order it to dispatch an event of your own that will be listened in the scope of its parent. I see you could do this in the container.

    First of all, you can reference the movie which has been loaded this way:

    PHP Code:
    var content:MovieClip;
    ldr.contentLoaderInfo.addEventListener(Event.COMPLETEcompleteHandler);

    // Executed when the load is complete
    function completeHandler(e:Event):void {
         
    content e.target.content as MovieClip;
         
    content.addEventListener("hasChanged"onContentChanged);
    }

    // Executed when the loaded movie has dispatched the event "hasChanged"
    function onContentChanged(e:Event):void {
        
    // do what you need with sBar

    Now inside your loaded movie, place this line on suitable when its content has changed:

    PHP Code:
    dispatchEvent(new Event("hasChanged")); 

  3. #3
    Senior Member
    Join Date
    Nov 2005
    Location
    Milan
    Posts
    119
    Thanks a million, that did the trick.

  4. #4
    Member
    Join Date
    Sep 2007
    Posts
    69
    Glad to help!

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