dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Buttons in a child movie won't work while loaded in parent movie

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    8

    Question Buttons in a child movie won't work while loaded in parent movie

    I have an external swf (sub.swf) that loads into my main swf. The external swf has buttons of its own...that I would like to use to load additional external swf's in its place. I click on the buttons and nothings happens.

    My current code is as follows:

    All of the following code is in my main.swf
    ================================

    //Location where the external SWFs will load
    var Xpos:Number=18;
    var Ypos:Number=10;

    //Movie variable
    var _externalMovie:MovieClip;

    //Loader variable
    var _swfContainer:Loader=new Loader();

    //The default external SWF that is to be loaded
    var defaultSWF:URLRequest=new URLRequest("swfs/sub.swf");

    //Execution of external SWF loading
    _swfContainer.load(defaultSWF);
    _swfContainer.x=Xpos;
    _swfContainer.y=Ypos;
    addChild(_swfContainer);
    _swfContainer.contentLoaderInfo.addEventListener(f lash.events.Event.COMPLETE, finishedLoading);

    //This is done afer the swf is loaded
    function finishedLoading(event:Event)
    {
    _externalMovie = MovieClip(_swfContainer.content);
    }

    //Universal Button Function
    function btnClickMain(event:MouseEvent):void {
    removeChild(_swfContainer);
    var newSWFRequest:URLRequest=new URLRequest("swfs/"+ event.target.name +".swf");
    _swfContainer.load(newSWFRequest);
    _swfContainer.x=Xpos;
    _swfContainer.y=Ypos;
    addChild(_swfContainer);
    }

    //Accessing the external SWF
    function activateExternalSWFelements(event:MouseEvent):void {
    _swfContainer.contentLoaderInfo.addEventListener(f lash.events.Event.COMPLETE, finishedLoading); //necessary to see if ext swf has loaded before accessing movie clip in ext swf

    function finishedLoading(event:Event){
    //now that external SWF has finished loading, we can manipulate it's elements
    _externalMovie.media.addEventListener(MouseEvent.C LICK, btnClickMain); //button in sub.swf with an instance name of media
    _externalMovie.archives.addEventListener(MouseEven t.CLICK, btnClickMain); //button in sub.swf with an instance name of archives
    }
    }

    ==================================

    External swf has no code except for a stop action.

    Any and all ideas are welcome. I pieced this current code based on various articles I've found on the net. I'm a newbie.....so bear with me.

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    well, first off i might try setting the stage focus to your loaded .swf. Sometimes that helps. Question :

    Is there any particular reason why you are assigning mousevents to children of a loaded swf ? I would maybe remove the event listeners from the parent class, and add some code to the loaded swf. Just dont add the listeners in the loaded swf until the stage added method is executed. It might be that scope is wrong ,the events might not be bubbling to the correct targets , or the actionscript is somehow being overwritten upon load.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    8

    Child Movies

    Thanks for the quick response AttackRabbit. As far as movements...are you referring to the x and y positioning variables? I wanted the child movies to load on top of my main swf in a particular location. I was under the impression those were needed.

    I had tried loading to external swf's to the "stage" by adding stage to the addChild code.... stage.addChild(_swfcontent). It didn't make a difference...but then again..I might have been missing some other key elements. Again....I'm a newbie.

    I originally had the event listeners on the loaded swf...........the problem I ran in to....was I couldn't get the already loaded default swf off the stage when the new ones loaded. They would load properly.........but layered on top of the default one. If I could simply add the event listeners to the loaded swf...and refer back to the universal button function in the main swf (btnClickMain)...that would be great!!! But I couldn't figure out how to get the code to point to the parent function properly.

    I read about making the code a public function..........then ran into issues as far as not having it within a package...and so on..and so on.

    Question: How would you add an event listener....and have it use variables declared in the parent movie? .......without having to re-declare all the variables again in the loaded swf?

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    8

    Got it to work

    Ok.......based on the following article .....I revamped my coding...and was able to accomplish what I was looking for. Here's what my working code looks like:

    //////MAIN FILE CODE

    stop();

    function changeMovies(nextMovie:String):void {

    removeChildAt(this.numChildren-1);
    loadMovie(nextMovie);
    }

    function loadMovie(nextMovie:String){
    var Xpos:Number=18;
    var YPos:Number=10;
    var loader:Loader=new Loader();
    var defaultSWF:URL Request=new URLRequest(nextMovie);

    loader.load(defaultSWF);
    loader.x=Xpos;
    loader.y=Ypos;
    addChild(loader);
    }

    loadMovie("sub.swf");



    /////CODE WITHIN SUB.SWF

    stop();

    function btnClickSWF(event:MouseEvent):void {

    MovieClip(this.parent.parent).changeMovies(event.t arget.name);
    }

    media.addEventListener(MouseEvent.CLICK, btnClickSWF);

    /////////////////////////////////////////


    Thanks to all.......especially the participants of the thread which helped me.

  5. #5
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    glad to hear you resolved your issue. what i meant was , that by loading an external swf, and adding functionality to movieclips inside that , swf , and using things like , .parent.parent , the object oriented benefits of as3 are kinda lost. Encapsulation is designed to make issues like these easier , so rather then loading a swf , with some movieclips that do nothing, actually write the inteface code inside of the sub swf, and use the event dispatcher , with bubbling phase set to true, to communicat to your parent movie. also setting the stage focus can be done by importing the Stage package and saying something like ,

    Stage.focus = myClipMC.

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