A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Loading Transitions from Array

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    17

    Loading Transitions from Array

    I have 3 buttons on the stage, and when one is clicked I would like it to load the corresponding swf from the array. Here's the script i'm working with.


    However I keep getting this error when I run the script (when a button is clicked)

    0
    1
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display:isplayObjectContainer/removeChild()
    at Smile/::clearCurrentSwf()
    at Smile/:nCompleteHandler()

    Any ideas?




    code:


    private var _numberOfSwfs:int;
    private var currentSwf;

    private function btnDown0 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf(0);
    }
    private function btnDown1 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf(1);
    }
    private function btnDown2 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf(2);

    }
    // **Loader**
    function clearCurrentSwf ():void {
    if (currentSwf != null) {
    removeChild (currentSwf);
    currentSwf = null;
    }
    }
    private function loadNextSwf (index:int):void {
    var _swfs:Array = ["1.swf", "2.swf", "3.swf"];
    var url = _swfs[index];
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest(url);
    mLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load (mRequest);
    currentSwf = mLoader;
    }
    function onCompleteHandler (loadEvent:Event) {
    clearCurrentSwf ();
    addChild (loadEvent.currentTarget.content);
    currentSwf = loadEvent.currentTarget.content;
    trace("the current swf is" + currentSwf);
    }
    function onProgressHandler (mProgress:ProgressEvent) {
    var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
    trace (percent);
    }

    Last edited by peteWaia; 05-22-2008 at 05:18 PM.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The Loader is not a child of that class object. Add the Loader before:
    addChild(mLoader);
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    17
    Do you mean:

    addChild (loadEvent.currentTarget.content);


    If i remove the clearCurrentSwf function from happening is works well. I basically need a way to remove the old content first, if another button is clicked.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Remove this line:
    addChild (loadEvent.currentTarget.content);
    and add
    addChild(mLoader); after mLoader.load (mRequest);
    I recommend you to create private static variable for the Loader in the beginning of your script:
    private static var mLoader:Loader;

    then later you can add this:
    if (mLoader != null) {
    removeChild (mLoader);
    }
    mLoader = new Loader();

    A local variable would not exist until it is declared but a class variable when declared before would exist but be null.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    17
    I really appreciate your quick replies and advise. I'm just kind of lost and am still getting the same results. Heres the script (trimmed up) as of now, without the clearCurrentSwf function being called. I'm a little confused on where to put it.



    code:

    package {
    import flash.events.*;
    import flash.display.*;
    import flash.text.*;
    import flash.net.*;
    import caurina.transitions.*;

    public class Smile extends MovieClip {
    private static var mLoader:Loader;
    private var _numberOfSwfs:int;
    private var currentSwf;

    public function Smile () {

    }

    private function btnDown0 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf (0);
    }
    private function btnDown1 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf (1);
    }
    private function btnDown2 (e:MouseEvent):void {
    menuPosition2 ();
    loadNextSwf (2);
    }
    // **Loader**
    private function loadNextSwf (index:int):void {
    var _swfs:Array = ["resume.swf", "resume.swf", "resume.swf"];
    var url = _swfs[index];
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest(url);
    mLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load (mRequest);
    addChild (mLoader);
    currentSwf = mLoader;

    }
    function onCompleteHandler (loadEvent:Event) {
    currentSwf = loadEvent.currentTarget.content;
    trace ("the current swf is" + currentSwf);

    }
    function onProgressHandler (mProgress:ProgressEvent) {
    var percent:Number =Math.round(mProgress.bytesLoaded/mProgress.bytesTotal*100);
    trace ("percent" + percent);
    }
    function clearCurrentSwf ():void {
    if (mLoader != null) {
    removeChild (mLoader);
    }
    mLoader = new Loader();

    }
    // ** END Loader**
    }
    }


  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    private function loadNextSwf (index:int):void {
    var _swfs:Array = ["resume.swf", "resume.swf", "resume.swf"];
    var url = _swfs[index];
    if (mLoader != null) {
    removeChild (mLoader);
    }
    mLoader = new Loader();
    var mRequest:URLRequest = new URLRequest(url);
    mLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load (mRequest);
    addChild (mLoader);

    }

    and remove the clearCurrentSwf function.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    May 2008
    Posts
    17
    Works perfectly. Thanks so much.

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