我正在製作遊戲,基本上是拖動遊戲,但是當我拖動我的對象時,此錯誤顯示「TypeError:錯誤#1034:類型強制失敗:無法將flash.display :: Loader @ 25aa6d41轉換爲flash.display.MovieClip。」我不知道如何調試它,因爲我是新來as3..please幫助TypeError:錯誤#1034:類型強制失敗:無法將flash.display :: Loader @ 25aa6d41轉換爲flash.display.MovieClip
這裏的
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);
}
對不可追蹤對象使用'trace'將不起作用。它會一直追蹤'[Object object]'。您需要使用調試器或跟蹤諸如「trace(obj是MovieClip)」之類的東西。 –
有幾次我得到[對象MovieClip]而不是[Object object],如果它是一個精靈,它會去[object Sprite]。但是如果你確實看到了[對象對象],那麼你就必須嘗試喬什所說的,它將評估爲真或假。 – mitim
非常感謝。這工作並增加了我對as3的知識 – Nok