A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: load external swf using loader class as3

  1. #1
    Member
    Join Date
    Feb 2009
    Posts
    30

    load external swf using loader class as3

    Hey

    I am pulling my hair out over this. I have a flash rotating banner script that I purchased, and wish to incorporate into my homepage. Instead of creating a page with HTML and adding the banner code as a separate object on the page, I wish to load it as an external swf into a movieclip on my stage. This is what the banner looks like:

    http://www.djsting.com/test2

    I have a page that I'm redesigning, it's located at:

    http://www.djsting.com/test

    I have a movieclip named load_banner. Inside the movieclip in its own frame, I put in the following code:

    Code:
    import flash.net.URLRequest;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    
    function startLoad()
    {
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest("banner.swf");
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest);
    }
    
    function onCompleteHandler(loadEvent:Event)
    {
            addChild(loadEvent.currentTarget.content);
    }
    function onProgressHandler(mProgress:ProgressEvent)
    {
    var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
    trace(percent);
    }
    startLoad();
    However when I test it, the images load off to the right hand side of the screen, not centered in the movie clip like I want...

    It's not acting at all like I want it to... can anyone help?

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    it doesn't look like you are setting the 'mLoader' positioning anywhere in that code.

    when you addChild(mLoader) to your stage you also need to set the mLoader.x and mLoader.y so it will position it where you want it.

  3. #3
    Member
    Join Date
    Feb 2009
    Posts
    30
    Hehe...thanks!!! I knew there was somewhere in that code to add positioning I just didn't know where!! Thanks for the tip!

  4. #4
    Member
    Join Date
    Feb 2009
    Posts
    30
    Hmm... I guess this wasn't as easy as I thought lol. The line of code states:
    Code:
    addChild(loadEvent.currentTarget.content);
    The mLoader class isn't defined in this function so I can't set the x any y parameters, it gives me an error. I tried to add the code right after
    Code:
    mLoader.load(mRequest);
    but it still ended up positioning it in the top left corner, with the buttons on the bottom right... very strange.

    Do you know where in this code to put in the X and Y? And if I do it for mLoader, or for the loadEvent used for the addChild? I'm such a flash n00b... my apologies lol.

    I will add, that when I test the movie, the output window pops up with 01... dunno why that's happening either. Should I be doing this from within a movieclip on the stage... or just loading it onto a specific area of my main stage? Again, thanks for your help with this!!
    Last edited by djsting; 04-26-2009 at 04:51 AM. Reason: missed something ;)

  5. #5

  6. #6
    Member
    Join Date
    Feb 2009
    Posts
    30
    This is what I was trying. This is placed on the actions layer of the main timeline, not contained within a movieclip.

    Code:
    import flash.net.URLRequest;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    
    function startLoad()
    {
    var mLoader:Loader = new Loader();
    var mRequest:URLRequest = new URLRequest("banner.swf");
    mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
    mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    mLoader.load(mRequest);
    mLoader.x = 37;
    mLoader.y = 34;
    }
    
    function onCompleteHandler(loadEvent:Event)
    {
            addChild(loadEvent.currentTarget.content);
    }
    function onProgressHandler(mProgress:ProgressEvent)
    {
    var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
    trace(percent);
    }
    startLoad();

  7. #7
    Member
    Join Date
    Feb 2001
    Posts
    56
    Since you've added it to the stage without creating a referencing in the class to it as an object - you have to go back to the display list to grab it. You could do this right after you add it.

    var tempDO: DisplayObject = getChildAt(0);
    tempDO.x = wherex
    tempDO.y = wherey

    -<>|TheMadFlasher|><-

  8. #8
    Member
    Join Date
    Feb 2009
    Posts
    30
    I'll def try that thanks!! I'll let you know if that did the trick

  9. #9
    Member
    Join Date
    Feb 2009
    Posts
    30
    Hmm... it's not seeming to work. I think the banner code has something in it that automatically adjusts the size based on the stage, which is why its appearing at the top left, and bottom right of the stage no matter what I do. How do I confine this to a movieclip?

  10. #10
    Member
    Join Date
    Feb 2001
    Posts
    56
    If some code inside the banner swf is adjusting it to the stage... you might have a real issue - that only getting to that code would fix. At that point setting some sort of listener that checks when it's moved - and then adjusting it accordingly, but again the internal code might be a bigger issue.

    You can try wrapping it in another movieclip first before adding it to your movie- but again might not work if it's internally referencing the stage.

    -<>|TheMadFlasher|<>-

  11. #11
    Junior Member
    Join Date
    May 2009
    Posts
    1
    Can I use external action script to load external swf into stage ?

    http://www.myboodesign.com

  12. #12
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    Quote Originally Posted by myboo View Post
    Can I use external action script to load external swf into stage ?

    http://www.myboodesign.com
    yes, but you can only access the stage from a loaded swf if its in the same domain as the swf that its loaded into.

    so if a swf is loaded from www.foo.com/some.swf

    and that swf load an external swf from www.bar.com/another.swf, the 'another.swf' will not be able to access the stage of 'some.swf'... directly. There are some work around if you need them.

Tags for this Thread

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