A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Loader Class + as a package

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3

    Loader Class + as a package

    I wanted to design a [relatively] all purpose class to load images and SWF's with a preloader MC and error handler.

    I made this BUT, when I load the MC I cannot control it.

    i.e. I load an SWF containing an animated GIF, but I cannot say mc.gotoAndStop();

    Example:
    PHP Code:
    package
    {
    public class 
    test extends MovieClip
        
    {
            private var 
    imgLoader:imageLoader;
            
            public function 
    test()
            {
    //to load the image into an MC 

    imgLoader = new imageLoader(imgPath,"Image name for Pre-Loader",myTarget,myWidth,myHeight,myX,myY);



    }}} 
    Usually for myTarget I use 'this'



    This is the class

    PHP Code:

    package
    {
        
    //list of our imports these are classes we need in order to
        //run our application.
        
    import flash.display.MovieClip;
        
    import flash.display.Stage;
        
    import flash.display.Loader;
        
    import flash.events.*;
        
    import flash.net.URLRequest;
        
    import flash.net.URLLoader;
     

     
        
    //our Engine class it extends MovieClip
        
    public class imageLoader extends MovieClip
        
    {
            private var 
    imageName:String;
            
            private var 
    imgLoader:Loader;
            private var 
    mc_preLoader:preLoader = new preLoader();
            private var 
    mcEr:JHerror = new JHerror(); // Error MC displays error and sends email to admin
            
            
    private var mc_target:Object;
            private var 
    imgWidth:Number;
             private var 
    imgHeight:Number;
            private var 
    imgX:Number;
            private var 
    imgY:Number;
            
            public function 
    imageLoader(url:String,img:String,target_mc:Object,Width:Number,Height:Number,X:Number,Y:Number):void 
            
    {
                
    imgWidth Width;
                
    imgHeight Height;
                
    imgX X;
                
    imgY Y;
                
    mc_target target_mc;
                
    imageName img;
                
                
    imgLoader=new Loader();
                
    imgLoader.load(new URLRequest(url));
                
    //Add a listener to update the preloader on the image's progress
                
    imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,imageLoading);
                
    //Adding a listener that will update the preloader once the image has been loaded
                   
    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
                
    imgLoader.addEventListener IOErrorEvent.IO_ERRORhandleIOError );
                
    imgLoader.addEventListener SecurityErrorEvent.SECURITY_ERRORhandleSecurityError ); 
                
    stage.addChild(mc_preLoader);
            }
     
            public function 
    imageLoaded(evt:Event):void 
            
    {
                 
    imgLoader.width imgWidth;
                 
    imgLoader.height imgHeight;
                 
    imgLoader.imgX;
                 
    imgLoader.imgY;
                 
                 
    mc_target.addChild(imgLoader);
                 
    mc_preLoader.killSelf();
                
                
            }
     
            public function 
    imageLoading(evt:ProgressEvent):void 
            
    {
                
    // Get the current download progress
                
    var loaded:Number=evt.bytesLoaded/evt.bytesTotal;
                
    // Send progress info to "preloader" movie clip
                
    updateProgress(loaded);
            }
     
            public function 
    updateProgress(percent:Number
            {
                
    percent Math.floor(percent*100);
                var 
    loadedString:String percent.toString();
                
    mc_preLoader.txtImagePreload.text "Loading " imageName " Please Wait " loadedString "  %";
            }
            function 
    handleIOError event:IOErrorEvent ):void
            
    {
                
    addChild(mcEr); // if error display error message mc
                
    mcEr.init("Store() : State() : IOErrorEvent catch: " event.text);
                
    trace "Load failed: IO error: " event.text );
            }
            function 
    handleSecurityError event:SecurityErrorEvent ):void
            
    {
                
    addChild(mcEr); // if error display error message mc 
                
    mcEr.init"Store() : State() : Load failed: Security Error: " event.text );
                
    trace "Load failed: Security Error: " event.text );
            }
            public function 
    killSelf():void
            
    {
                
    parent.removeChild(this);
            }
        }

    Thanks for any help
    Last edited by 061375; 01-10-2011 at 12:39 PM. Reason: My code was incorrect and would have errored

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Add
    addChild(imgLoader);
    - The right of the People to create Flash movies shall not be infringed. -

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