TypeError: Error #1034: Type Coercion failed:cannot convert flash.display::Loader@25aa6d41 to flash.display.MovieClip

StackOverflow https://stackoverflow.com/questions/22161820

  •  19-10-2022
  •  | 
  •  

Question

I'm making a game, basically a dragging game but when i drag my objects this error shows up "TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@25aa6d41 to flash.display.MovieClip." I don't know how to debug it cause i'm new to as3..please help

here's the code

enter code here
var xmlRequest:URLRequest = new URLRequest("items.xml");
var xmlLoader:URLLoader = new URLLoader(xmlRequest);
var xmlFile:XML;
var xcoord:Number = 24;
var ycoord:Number = 157;
var colorArray:Array = new Array();//array for picture directory
colorArray[1]="images/blue/blue_";
colorArray[2]="images/green/green_";
colorArray[3]="images/indigo/indigo_";
colorArray[4]="images/orange/orange_";
colorArray[5]="images/pink/pink_";
colorArray[6]="images/red/red_";
colorArray[7]="images/violet/violet_";
colorArray[8]="images/yellow/yellow_";
var totalBlue:Number;
var totalGreen:Number;
var totalIndigo:Number;
var totalOrange:Number;
var totalPink:Number;
var totalRed:Number;
var totalViolet:Number;
var totalYellow:Number;
var total:Array = new Array();
var pb:Array=new Array();
var index:Array = new Array();//array for picking picture number
var indexc:Array = new Array();//array for picking picture directory

xmlLoader.addEventListener(Event.COMPLETE,xmlLoadComplete);

function xmlLoadComplete(e:Event):void{

xmlFile = new XML(xmlLoader.data);
total[1]=xmlFile.blue.image.length();
total[2]=xmlFile.green.image.length();
total[3]=xmlFile.indigo.image.length();
total[4]=xmlFile.orange.image.length();
total[5]=xmlFile.pink.image.length();
total[6]=xmlFile.red.image.length();
total[7]=xmlFile.violet.image.length();
total[8]=xmlFile.yellow.image.length();
var tempArray:Array = new Array();
var ind1:Array=randomArray(total[1]);
var ind2:Array=randomArray(total[2]);
var ind3:Array=randomArray(total[3]);
var ind4:Array=randomArray(total[4]);
var ind5:Array=randomArray(total[5]);
var ind6:Array=randomArray(total[6]);
var ind7:Array=randomArray(total[7]);
var ind8:Array=randomArray(total[8]);

indexc = randomArray(8);

var count:int=1;
var count2:int=0;
for(var i:int=1;i<=24;i++)//on xml load completes, creates a 2x12 picture      table/picBox
{
    pb[i]=new picBox();//create a userdefined movieclip
    pb[i].x=xcoord;
    pb[i].y=ycoord;
    pb[i].buttonMode=true;
    addChild(pb[i]);
    xcoord+=pb[i].width+2;//sets x starting point of pb,value 2 = space between movieclips
    if(i==12)
    {
        xcoord=24;
        ycoord+=pb[i].height+10;//sets y starting point of pb,10=space
    }

    if(count==9)
    {
        count=1;
        indexc=randomArray(8);
    }
    else
    count2=0;
    if(indexc[count-1]==1)index=ind1;
    else if(indexc[count-1]==2)index=ind2;
    else if(indexc[count-1]==3)index=ind3;
    else if(indexc[count-1]==4)index=ind4;
    else if(indexc[count-1]==5)index=ind5;
    else if(indexc[count-1]==6)index=ind6;
    else if(indexc[count-1]==7)index=ind7;
    else if(indexc[count-1]==8)index=ind8;
    trace(indexc);
    var loader:Loader = new Loader();//loads the file on location...
    loader.load(new URLRequest(colorArray[indexc[count-1]]+index[count2]+".png"));//load random image from random images folders
    index.splice(0,1);
    trace(index);//trace index
    trace(count);//trace count
    count++;
    pb[i].addChild(loader);//adds the picture on the picBox
    pb[i].addEventListener(MouseEvent.MOUSE_DOWN,dragObject);
    pb[i].addEventListener(MouseEvent.MOUSE_UP,releaseObject);
}
 }
function dragObject(event:MouseEvent):void { 
var item:MovieClip=MovieClip(event.target); 
item.startDrag(); 
var topPos:uint=this.numChildren-1; 
this.setChildIndex(item, topPos);    
}  
function releaseObject(event:MouseEvent):void{ 
var item:MovieClip=MovieClip(event.target); 
item.stopDrag();       
if (box1_mc.hitTestPoint(item.x,item.y)) { 
    item.x=33; 
    item.y=58; 
} else { 
   //item.x=orig1X; 
   //item.y=orig1Y; 
}  
}

//returns Array with random non repeating number
function randomArray(len:int):Array {
var tempArray:Array=new Array();
var resultArray:Array=new Array();
for(var i:int=1;i<=len;i++)
{
    tempArray[i]=i;
}
var mult:int=len;
for(var i2:int=0;i2<len;i2++)
{
    var randnum:int = Math.floor (Math.random () * mult+1);
    var randomNum:int = tempArray [randnum];
    resultArray.push (randomNum);
    tempArray.splice (randnum, 1);
    mult--;
}
return (resultArray);
 }
Was it helpful?

Solution

It's saying that somewhere in your code, you're tying to use a Loader object where a MovieClip object should be. Skimming through your code, I see two places where you've casted an object to use as a Movieclip (where a Loader would not work). Try tracing out event.target at those spots to see if it is a MovieClip object or the Loader object you have within.

If it shows the loader object, it's probably what is causing the error. Try changing event.target to event.currentTarget.

The way I understand the difference is event.target is usually the object you clicked on (the Loader object within the MovieClip object), while event.currentTarget is the object that is processing the event that has the event listener, which is the MovieClip you're after in this case. (Anyone, feel free to correct me if I'm wrong)

As a side note, if you're using the Flash IDE, you can also press 'control + shift + enter' (instead of just control + enter) to use the debugger for testing, which usually can show you the exact line that gave the error as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top