【问题标题】:TypeError: Error #1034: Type Coercion failed:cannot convert flash.display::Loader@25aa6d41 to flash.display.MovieClipTypeError:错误 #1034:类型强制失败:无法将 flash.display::Loader@25aa6d41 转换为 flash.display.MovieClip
【发布时间】:2014-03-04 02:42:33
【问题描述】:

我正在制作一个游戏,基本上是一个拖动游戏,但是当我拖动我的对象时,此错误显示“TypeError:错误 #1034:类型强制失败:无法将 flash.display::Loader@25aa6d41 转换为 flash.display。影片剪辑。”我不知道如何调试它,因为我是 as3 的新手..请帮助

这是代码

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);
 }

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    它是说在您的代码中的某个地方,您要在 MovieClip 对象应该存在的地方使用 Loader 对象。浏览您的代码,我看到您在两个地方投射了一个对象以用作电影剪辑(加载器无法工作)。尝试在这些位置追踪event.target 以查看它是MovieClip 对象还是您拥有的Loader 对象。

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

    我理解差异的方式是 event.target 通常是您单击的对象(MovieClip 对象中的 Loader 对象),而 event.currentTarget 是正在处理具有事件侦听器的事件的对象,即在这种情况下,您需要的 MovieClip。 (任何人,如果我错了,请随时纠正我)

    附带说明,如果您使用的是 Flash IDE,您还可以按“control + shift + enter”(而不仅仅是 control + enter)来使用调试器进行测试,这通常可以显示确切的也给出了错误的行。

    【讨论】:

    • 在不可追踪的对象上使用trace 将不起作用。它将始终追踪[Object object]。您需要使用调试器或跟踪 trace(obj is MovieClip) 之类的东西。
    • 有几次我得到[object MovieClip]而不是[Object object],如果它是一个精灵,它会去[object Sprite]。但是,如果您确实看到 [object Object],那么您将不得不尝试 Josh 所说的,这将评估为真或假。
    猜你喜欢
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    相关资源
    最近更新 更多