拖放目标

时间:2014-09-13 09:12:39

标签: actionscript-3 flash drag-and-drop

我在flash as3中有点新功能,但我正在做的课件应用程序涉及将文字拖入适当的目标,如果匹配正确,还包括20秒计时器,应该进行练习。但我的问题是在我将正确的单词拖到他们的位置并得到相关的分数(我使用10个标记每个正确的答案)仍然我可以使用每个拖动的单词一次又一次地拖动单词和分数(即我想无法拖动来自拖动)使用户得分超过更高的分数(即90分,因为有9个字要拖动)。

这是我的编码;

 stop();    
    import flash.events.MouseEvent;   
    import flash.events.TimerEvent;    
    import flash.utils.Timer;    
    var myscore1 = 0;  score.text = myscore1+"";

    var hitArray:Array = new Array(love_tgt, am_stud_tgt, living_tgt, is_improv_tgt, speak_tgt,
                                   means_tgt, are_help_tgt, want_tgt, miss_tgt);    
    var dropArray:Array = new Array(love, am_stud, living, is_improv, speak, means, are_help,
                                    want, miss);    
var positionsArray:Array = new Array();

    for (var i:int = 0; i < dropArray.length; i++) {
         dropArray[i].buttonMode = true;
         dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
         dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);

         positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
    }

    function mdown(e:MouseEvent):void {
        e.currentTarget.startDrag();
        setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
    }

    function mUp(e:MouseEvent):void {   
    var dropIndex:int = dropArray.indexOf(e.currentTarget);   
    var target:MovieClip = e.currentTarget as MovieClip;    
    target.stopDrag();

        if (target.hitTestObject(hitArray[dropIndex])) {
                myscore1+=10;
                score.text = myscore1+""; 

                target.x = hitArray[dropIndex].x;
                target.y = hitArray[dropIndex].y;
            }else{
               target.x = positionsArray[dropIndex].xPos;
               target.y = positionsArray[dropIndex].yPos;
            }    
    }


    var myTimer1:Timer = new Timer(1000);    
    myTimer1.addEventListener(TimerEvent.TIMER, countdownHandler1);    
    function  countdownHandler1(event:TimerEvent):void{       
    countdown.text = 20-myTimer1.currentCount+" s remaining";
             if(myTimer1.currentCount==20){
                gotoAndStop(46);
             }      }  

      myTimer1.start();


    }

1 个答案:

答案 0 :(得分:0)

您仍然可以拖动对象,因为它们仍然有事件侦听器。您需要做的就是删除它。

if (target.hitTestObject(hitArray[dropIndex])) {
      myscore1+=10;
      score.text = myscore1+""; 

       target.x = hitArray[dropIndex].x;
       target.y = hitArray[dropIndex].y;

       //add these 2 lines---------------------------------------------
       target.removeEventListener(MouseEvent.MOUSE_DOWN, mdown);
       target.removeEventListener(MouseEvent.MOUSE_UP, mUp);
       //--------------------------------------------------------------
 }else{
      target.x = positionsArray[dropIndex].xPos;
      target.y = positionsArray[dropIndex].yPos;
 }