2014-03-13 44 views
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); 
+0

這將是有益的,如果你正確地格式化代碼(縮進)。 – bwroga

+0

感謝那個...我是新來的stackoverflow – Nok

+0

waw你的函數返回函數是如此可怕;)我認爲這將是更好地把你的變量indx1,indx2,indx,origX和origY在一些班級變量和使用真正的命名函數作爲事件處理程序而不是匿名函數。在您提供的代碼上,您無法調用dragObject和releaseObect方法。 –

回答

0

部分你有非常具體的代碼......這裏工作示例拖動對象,如果達到屏幕的右側將被停止。正如你所看到的,在我的例子中,我不使用像你這樣的結構(函數生成另一個函數)。注意onDownonMouseUponMoveCursor方法:

package { 

    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 

    public class StackOverflow extends Sprite { 

     private var _dragObject:Sprite; 

     public function StackOverflow() { 
      addEventListener(Event.ADDED_TO_STAGE, onAdded); 
     } 

     private function setup():void { 
      var randomObject:Sprite = createObject(); 

      addChild(randomObject); 

      //Place it in center of scene 
      randomObject.x = stage.stageWidth >> 1; 
      randomObject.y = stage.stageHeight >> 1; 

      //Register mouse down for drag 
      randomObject.addEventListener(MouseEvent.MOUSE_DOWN, onDown); 
     } 

     private function onDown(e:MouseEvent):void { 
      //By moving mouse will check current position 
      stage.addEventListener(MouseEvent.MOUSE_MOVE, onMoveCursor); 
      stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 

      _dragObject = e.currentTarget as Sprite; 

      _dragObject.startDrag(); 
     } 

     private function onMouseUp(e:MouseEvent):void { 
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveCursor); 
      stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); 

      if (_dragObject != null) { 
       _dragObject.stopDrag(); 
       _dragObject = null; 
      } 
     } 

     private function onMoveCursor(e:MouseEvent):void { 
      //Assert that right side is end of the screen 
      if (_dragObject.width + _dragObject.x >= stage.stageWidth) { 
       trace("Gotcha!") 
       onMouseUp(null); 
      } 
     } 

     private function createObject():Sprite { 
      var result:Sprite = new Sprite(); 
      result.graphics.beginFill(Math.random() * 0xFFFFFF); 
      result.graphics.drawRect(0, 0, 20, 20); 
      result.buttonMode = true; 
      result.tabEnabled = false; 
      return result; 
     } 

     private function onAdded(e:Event):void { 
      removeEventListener(Event.ADDED_TO_STAGE, onAdded); 

      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 

      setup(); 
     } 
    } 
} 
相關問題