actionscript-3loadermovieclip

Actionscript 3.0 Dragging Loader image but getting error that it can't be converted to a movieclip


I have a script that loads an external image into the stage(I have the addChild() script somewhere else) but I keep getting an error that says:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@b1b7101 to flash.display.MovieClip. at project1_fla::MainTimeline/drag()

var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://i54.tinypic.com/anom5d.png"));
my_loader.addEventListener(MouseEvent.MOUSE_DOWN, drag);

function drag(event:MouseEvent):void{
 var my_loader:MovieClip = MovieClip(event.target);
 my_loader.startDrag()
 my_loader.scaleX = my_loader.scaleY = .95;

What I do to make the image draggable? *(The image is loaded when the swf starts but the image won't because I put the addChild() in a if() statement. Could it be that since the image isn't loaded, it can't be a movieclip?)


Solution

  • There are a couple of errors with the following line.

    var my_loader:MovieClip = MovieClip(event.target);
    

    Firstly , the event target is of type Loader so you won't be able to coerce it into a MovieClip. Secondly, you would typically do this type of coercion when loading a swf , but you're loading a png!

     var container:Sprite = new Sprite();
     addChild( container);
    
     my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
     my_loader.load(new URLRequest("http://i54.tinypic.com/anom5d.png"));
    
     function onLoadingComplete(event:Event):void
     {
        container.addChild( event.currentTarget.loader.content );
        container.addEventListener(MouseEvent.MOUSE_DOWN, drag);
        //remove the listener here
     }
    
     function drag(event:MouseEvent):void{
         container.startDrag()
         container.scaleX =  0.95;
         container.scaleY = 0.95;
      }