2014-03-04 102 views
0

我正在制作游戏,基本上是拖动游戏,但是当我拖动我的对象时,此错误显示“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); 
} 

回答

1

它说在你的代码的某个地方的代码,你绑到使用Loader MovieClip对象应该在的对象。浏览你的代码,我看到两个地方你已经铸造了一个对象用作Movieclip(一个Loader不起作用)。尝试在这些位置追查event.target以查看它是否是MovieClip对象或您内部的Loader对象。

如果显示加载器对象,则可能是导致错误的原因。尝试将event.target更改为event.currentTarget

我理解上的差异是event.target通常是你(MovieClip对象中的Loader对象)点击对象,而event.currentTarget是处理具有事件侦听器的事件的对象,这是影片剪辑你的方式在这种情况之后。 (任何人,如果我错了,随时纠正我)

作为一个方面说明,如果你使用的是Flash IDE,你也可以按'control + shift + enter'(而不是控制+输入)使用调试器进行测试,这通常会向您显示出现错误的确切行。

+0

对不可追踪对象使用'trace'将不起作用。它会一直追踪'[Object object]'。您需要使用调试器或跟踪诸如“trace(obj是MovieClip)”之类的东西。 –

+0

有几次我得到[对象MovieClip]而不是[Object object],如果它是一个精灵,它会去[object Sprite]。但是如果你确实看到了[对象对象],那么你就必须尝试乔什所说的,它将评估为真或假。 – mitim

+0

非常感谢。这工作并增加了我对as3的知识 – Nok

相关问题