A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Create new MovieClipLoader dynamically

  1. #1
    Junior Member
    Join Date
    Sep 2004
    Posts
    19

    Create new MovieClipLoader dynamically

    How do you create new MovieClipLoader instances dynamically? Like supposing I want to create 20 new MovieClipLoader using while with names "mcl1", "mcl2", "mcl3...", how does it work? I have my code below but I keep getting syntax errors

    PHP Code:
    var i:Number 1;
    while(
    <= 20){
    var 
    _root["mcl" i]:MovieClipLoader = new MovieClipLoader(); //error in this line
    i++;

    Is there a way to do this or should I write all 20 one by one instead?

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    You only need to use this code:
    PHP Code:
    var i:Number 1
    while(
    <= 20){ 
    var 
    mcLoader:MovieClipLoader = new MovieClipLoader();
    i++; 

    By specifying "new" it will create a new object each loop. Why are you creating all of these? Im sure there may be a better way to approach it :d

  3. #3
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    try leaving off the 'var' and data type assignment

    _root["mcl" + i] = new MovieClipLoader();

    _b

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    Just push them into an array like this:
    Code:
    var loaders:Array = new Array();
    for(var i:Number=0; i<20; i++)
    {
         loaders.push(new MovieClipLoader());
    }
    Now why do you need 20 instances of the MovieClipLoader class? It sounds like you are doing something wrong.

  5. #5
    Junior Member
    Join Date
    Sep 2004
    Posts
    19
    Quote Originally Posted by MyFriendIsATaco
    Now why do you need 20 instances of the MovieClipLoader class? It sounds like you are doing something wrong.
    Actually, you're right, I may be approaching this thing all wrong. Here's my scenario maybe you guys can tell me how to go about it:

    I'm loading 20 diff SWFs where all of them would be displayed simultaneously on the stage. So I thought I'd make 20 MovieClipLoaders and 20 Listeners one for each loaded movie. Am I doing it wrong? How do you think I should go about this?

  6. #6
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    You can do that all from one MovieClipLoader instance. That's what I assumed you were attempting to do.

  7. #7
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    I have something similar I am using to load all of my images for a slideshow and display the progress, here is the meat of my code for it.
    PHP Code:
    loadIntoClip("file1.swf"target_mcmain_preloader);
    loadIntoClip("file2.swf"target_mcmain_preloader);
    loadIntoClip("file3.swf"target_mcmain_preloader);
    loadIntoClip("file4.swf"target_mcmain_preloader);

    function 
    loadIntoClip(e:Stringtarget:MovieClippreloader:MovieClip) {
        var 
    mc_loader:MovieClipLoader = new MovieClipLoader();
        var 
    shhh:Object = new Object();
        
        
    mc_loader.loadClip(etarget);
        
    mc_loader.addListener(shhh);
        
        
    shhh.onLoadStart = function(target:MovieClip) {
            
    preloader._visible true;
        }
        
    shhh.onLoadProgress = function(target:MovieClip,lBytes:Number,tBytes:Number) {
            
    percent Math.round(int((lBytes/tBytes)*100));
            
    preloader.loadtext.text "Loading: " percent "%";
        }
        
    shhh.onLoadComplete = function(target:MovieClip) {
            
    preloader._visible false;
        }


  8. #8
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    Do you guys forget that you can just call loadClip() over and over again on the same instance and attach one listener?

    On the listener callback function, it returns the target MovieClip. So you can differentiate between the calls.

    [edit] Actually, take a look at this MultiClipLoader class I wrote: http://svn.ydekproductions.com/codeb...iClipLoader.as It may help you out.[/edit]
    Last edited by MyFriendIsATaco; 01-11-2008 at 03:14 PM.

  9. #9
    Junior Member
    Join Date
    Sep 2004
    Posts
    19
    Quote Originally Posted by sstalder
    I have something similar I am using to load all of my images for a slideshow and display the progress, here is the meat of my code for it...
    I looked at your code and now I see the logic behind using the MovieClipLoader class. I see now how stupid it was of me to create 20 instances of it when 1 was just enough.

  10. #10
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    sstalder, your code is not going to produce correct results at all if you are loading in a series of images and want to show one total progress bar. Each loading progress is updating the same text field, and being overwritten by each other file that calls the onLoadProgress callback.

    Also, you are using multiple instances of moviecliploader.

  11. #11
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    My code was more for having multiple preloaders. For example if he wants to load 50 images and have each image show it's own preloader.

  12. #12
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    You can still better and more efficiently do that by only using one instance of the MovieClipLoader class, with one set of callback functions. Each onLoadProgress call supplies the corresponding target MovieClip.

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