0
votes

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?)

2

2 Answers

1
votes

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;
  }
0
votes
var my_loader:Loader = new Loader();
addChild(my_loader);
my_loader.addEventListener(MouseEvent.MOUSE_DOWN, drag);
my_loader.load(new URLRequest("http://i54.tinypic.com/anom5d.png"));
function drag(event:MouseEvent):void{
 my_loader.startDrag()
 my_loader.scaleX =  0.95;
 my_loader.scaleY = 0.95;
}