0
當「item」位於正確的位置時,禁用拖動時出現問題。我試圖使用e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject)
,但似乎沒有任何反應。我認爲它不能正常工作,因爲我想刪除的事件不是`dragObject,但它返回的函數...請幫助; 這裏是我的代碼當它位於正確的位置時禁止拖動對象
function dragObject(indx1:int,indx2:int):Function {
return function(event:MouseEvent){
var item:MovieClip=MovieClip(event.currentTarget);
item.startDrag();
var topPos:uint=item.parent.numChildren-1;
var itemSound:Sound = new Sound();
itemSound.load(new URLRequest("sounds/"+dirArray[indx1]+indx2+".mp3"));
if(!activeSound)
{
itemSound.play();
activeSound=true;
}
activeSound=false;
item.parent.setChildIndex(item, topPos);
}
function releaseObject(indx:int,origX:int,origY:int):Function{
return function(e:MouseEvent):void{
var item:MovieClip=MovieClip(e.currentTarget);
item.stopDrag();
trace(indx);
if(indx==1)
{
if (box5_mc.hitTestPoint(item.x,item.y)) {
if(insideBox5==1){
item.x=73;//2nd locations
item.y=298;
myBell.play();
}
else if(insideBox5==2){
item.x=90;//3rd locations
item.y=267;
myBell.play();}
else{
item.x=32; //1st locations
item.y=268
myBell.play();
}
insideBox5++;
e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,dragObject);
correctItems++;
}
else {
item.x=origX;
item.y=origY;
myBoing.play();
} }
我已經更新了這篇文章,包括在那裏我把它叫做,它實際上是在我的主要功能。這裏有通話功能dragObject和releaseObject
var itemImage:Loader = new Loader();
//loads the file on location...
itemImage.load(new URLRequest("images/"+dirArray[indexc[count-1]]+index[count2]+".png"));//load random image from random images folders
var functionOnDrag:Function = dragObject(indexc[count-1],index[count2]);
index.splice(0,1);
var functionOnRelease:Function = releaseObject(indexc[count-1],tempx-42,tempy);
trace(index);//trace index
trace(count);//trace count
count++;
pb[i].addChild(itemImage);//adds the picture on the picBox
pb[i].addEventListener(MouseEvent.MOUSE_DOWN,functionOnDrag);
pb[i].addEventListener(MouseEvent.MOUSE_UP,functionOnRelease);
這將是有益的,如果你正確地格式化代碼(縮進)。 – bwroga
感謝那個...我是新來的stackoverflow – Nok
waw你的函數返回函數是如此可怕;)我認爲這將是更好地把你的變量indx1,indx2,indx,origX和origY在一些班級變量和使用真正的命名函數作爲事件處理程序而不是匿名函數。在您提供的代碼上,您無法調用dragObject和releaseObect方法。 –