DISQUS

*drawlogic: HOWTO: Using Loaders in AS3

  • cksachdev · 2 years ago
    cool introduction to loader ..
  • mode · 2 years ago
    kewl
    so if i wantd to load an external swf and display it twice on the screen, with different dimensions, how could that be done? could i make a copy of the loader?
  • drawk · 2 years ago
    Mode you could load the content into two Sprites. So instead of just addChild you would first add a sprite, then another and attach it to those, then add them to the main stage. Then you can size the Sprites as containers.
  • bobcat goldthwait · 2 years ago
    I have a list of images that i've looped overand loaded with the Loader class, but I'm having a tough time assigning them unique names.
    I want unique names because I want to know what loader the user clicks on (using mouse event handling).
    Is there a way to assign loaders a name or some unique tag so i can retrace what the user clicked on?
  • drawk · 2 years ago
    Sounds like you need aurthur debert's bulk loader. This makes handling many loaders very simple.

    http://www.stimuli.com.br/trane/2007/nov/25/loa...
  • Xavier · 1 year ago
    Hey all, what do i do if i have an exsisting movie clip, and i want to load an external swf from a url into it?

    Thanks

    Xavier
  • ryan · 1 year ago
    You can do it in the movieclip but if you need to do it from the container (load one in then load another movie into that) you will have to get a reference to it and then you can just [obj].addChild like any other object. There are lots more ways to do it, it really depends on where the controller is at. This sample loads into itself, you can easily get the loader and add the child to an object already in your stage or loaded in.
  • Ashok · 1 year ago
    hi ,

    I have an issue .I have to load an dynamically load images into movie clip in flash cs3 as3.

    The code is available in the link: http://drawlogic.com/2007/09/20/howto-using-loa...

    with the code

    1. var request:URLRequest = new URLRequest("content.swf");
    2. var loader:Loader = new Loader();
    3.
    4. loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
    5. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    6.
    7. function loadProgress(event:ProgressEvent):void {
    8. var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
    9. percentLoaded = Math.round(percentLoaded * 100);
    10. trace("Loading: "+percentLoaded+"%");
    11. }
    12. function loadComplete(event:Event):void {
    13. trace("Complete");
    14. }
    15.
    16. loader.load(request);
    17. addChild(loader);

    But only one image can be uploaded I want to load External IMAGES into a Movie Clips.

    Pease help me in this.

    With Regards
    AShok
  • common sense · 1 year ago
    if you read event descriptions above, INIT is when the external is actually in a 'ready' state... COMPLETE you would use to inject variables required into an external asset if they were desired/required.
  • coderx · 1 year ago
    good tutorial, just if addChild() could be in loadComplete -function.

    And your pages look WERY PERFECT!!!!! in my taste..
  • Таракан · 1 year ago
    Нифтяк!
  • luke · 1 year ago
    hey there, think someone else had this question as well re: using loaders to load images into two different places. but i've tried using sprites, attaching the loader to the sprite, then the sprite to the MC, and it still doesn't work - when i call loader again, the first image disappears. :-) i've got it working with the code below, but i'm sure this can be improved. any suggestions?

    // load in images dynamically
    var leftloader:Loader = new Loader();
    leftloader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
    leftloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
    var rightloader:Loader = new Loader();
    rightloader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
    rightloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

    function loadProgress(event:ProgressEvent):void {
    var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
    percentLoaded = Math.round(percentLoaded * 100);
    trace("Loading: "+percentLoaded+"%");
    }
    function loadComplete(event:Event):void {
    trace("Complete");
    }

    var curimg=0;
    function loadImage(thisframe) {
    curimg++;
    var request:URLRequest = new URLRequest("images/img"+curimg+".jpg");
    if (thisframe=="leftframe") {
    leftloader.load(request);
    leftframe.holder.addChild(leftloader);
    trace ("load left");
    } else {
    rightloader.load(request);
    rightframe.holder.addChild(rightloader);
    }
    }
    loadImage("leftframe");
  • ToryHargro.com · 1 year ago
    Hey Guys, I'm having a huge problem with loading an external .swf that includes an FLV video file. No matter what I do, myoader.unload(); or myloader.removeChild(); Nothing seems to make the video stop playing. It's dissappears but the audio continues to loop in the background,

    I managed to patch this using the flash.media.SoundMixer.stopAll(); methoid but this is ONLY a patch. My memory is getting killed!

    It appears adobe knows about this problem but please tell me someone has a solution!
  • Sosh · 1 year ago
    So, what you end up with is an object of type Loader? And you then use that like a sprite or movie clip? That's pretty ugly. Would have been better if the loader mechanism left you with a sprite or something.
  • ks61 · 1 year ago
    In actionScript 2.0 i used to load movies at level_0 to replace original. How can i do that with a loader?
    Must i (can i) unload the parent?
  • drawk · 1 year ago
    Hey ks61,

    Yes the new Display Object Tree allows you to load in objects into the display tree with addChild() on amy movie clip or the stage. If you addChild it will simple append the object. If you want to clear out the old one you can removeClip() or removeClipAt(index) before loading in the same location. You can removeClipAt(0) for clips that have no children to remove it from the display tree.
  • ks61 · 1 year ago
    Thanks for yor reaction, but ...

    removeClip() and removeClipAt() are not standard Flash actions are they?
    I cannot find them in the help-function.
  • drawk · 1 year ago
    Oh man I can't believe I said that.. hehe working on some AS2 with old school removeClip. Sorry yes it is removeChild() and removeChildAt() they are part of any object that inherits from DisplayObject such as flash.display.MovieClip. The whole display object tree is based on objects with children. So it is addChild and removeChild etc. Sorry for the confusion, late...
  • ks61 · 1 year ago
    tried and tried ...

    with removeChildAt(0) i remove a child in the loaded movie
    with parent.removeChildAt(0) i expected a small success but instead Flash crashes. (for some of you that maybe very logical, i simply copied it from another forum)

    there is no such thing as removeParent()

    A solution may be to start with an empty movie that loads another movie in a targetclip. The loaded movie can with a mouseclick load another movie in the targetclip. Sounds simple, but does not work as i expect it. Any suggestions?
  • ks61 · 1 year ago
    A solution may be to start with an empty movie that loads another movie in a targetclip. The loaded movie can, with a mouseclick, load another movie in the targetclip. Sounds simple, but does not work as i expect it. Any suggestions?

    i tried this:

    loadOther_btn.addEventListener(MouseEvent.MOUSE_UP, loadOther);

    function loadOther(event:MouseEvent):void {
    parent.request = new URLRequest("otherMovie.swf");
    parent.loader.load(request);
    }
  • ks61 · 1 year ago
    tried and tried and found it:

    var mRequest:URLRequest;
    var loader:Loader= new Loader();

    load400_btn.addEventListener(MouseEvent.MOUSE_UP, loadOther);

    function loadOther(event:MouseEvent):void {
    removeChildAt(0)
    mRequest = new URLRequest("otherMovie.swf");
    loader.load(mRequest);
    addChild(loader);
    }
  • ks61 · 1 year ago
    Another correction (the final one it seems) because you need to remove ALL children, in the other example i removed just one:

    var mRequest:URLRequest;
    var loader:Loader= new Loader();

    loadOther_btn.addEventListener(MouseEvent.MOUSE_UP, loadOther);

    function loadOther(event:MouseEvent):void {
    while (numChildren>0)
    {
    removeChildAt(0);
    }
    mRequest = new URLRequest("otherMovie.swf");
    loader.load(mRequest);
    addChild(loader);
    }
  • DDDDepressionnnn · 1 year ago
    Depression Depression Depression aaaaaaaa
    HEEEEELP :( :( :(
    I hate winter! I want summer!
  • wintervssummer · 1 year ago
    I very much love summer :)
    Someone very much loves winter :(
    I Wish to know whom more :)
    For what you love winter?
    For what you love summer? Let's argue :)
  • Dominik · 12 months ago
    Hi!
    Thanks for this! I got one question and would be really happy if you knew the answer.

    i'm loading 50 thumbs at once. Listener is set.
    thumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,thumbsLoaded);

    i want to know name of just loaded loader

    function thumbsLoaded(evt:Event):void
    {
    thumbLoader.name ??
    }

    all i figure out so far is i can see file url through contentLoaderInfo like this
    trace(evt.target.url);

    it would be great if there was a chance to set name of that loader and later get it when it's loaded.

    any hints ?

    cheers
    Dominik
    http://www.delay.pl/
  • drawk · 12 months ago
    Dominik,

    You probably want to write up us use something like http://code.google.com/p/bulk-loader/. This will help you with large sets of data to load and finding them.
  • hr · 10 months ago
    if the requested URL not found then I want to load my default file
    Can you please tell me the code for that
  • dawduefgelf · 8 months ago
    Хорошо пишете. Надеюсь, когда-нибудь увижу нечто подобное и на своем блоге…
  • helefyv · 7 months ago
    Хорошо пишете. Учились где-то или просто с опытом пришло?
  • virabux · 7 months ago
    Очень интересно. Но чего-то не хватает. Может быть, стоит добавить каких-нибудь картинок или фото?
  • hebygaf · 7 months ago
    Вот решил вам немного помочь и послал этот пост в социальные закладки. Очень надеюсь ваш рейтинг возрастет.
  • kefasot · 7 months ago
    Блог в ридер однозначно
  • wechate · 7 months ago
    Добавил в свои закладки. Теперь буду вас намного почаще читать!
  • jloa · 3 months ago
    Guys, those that needed to re-use e.i. make a copy of a loader after load - grab my LoaderUtil class
    the class archived: http://chargedweb.com/labs/files/LoaderUtil.zip
    find more useful stuff at my labs.

    Cheers