0

我正在尝试在 ActionScript 中创建一个函数,该函数在将可拖动的山墙对象放在另一个对象上时触发一个事件。

var hits = 0;

// Register mouse event functions
answer_j.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
answer_j.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_e.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_e.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_m.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_m.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_b.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_b.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_a1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_a1.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_t.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_t.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_a2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_a2.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

answer_n.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
answer_n.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

// Define a mouse down handler (user is dragging) 
function mouseDownHandler(evt:MouseEvent):void
{   
    var object = evt.target;    
    // limit dragging to the area inside the canvas     
    object.startDrag(); 
}

function mouseUpHandler(evt:MouseEvent):void {  
    var obj = evt.target;   
    // obj.dropTarget will give us the reference to the shape of    
    // the object over which we dropped the circle.     
    var target = obj.dropTarget;    
    // If the target object exists the we ask the test_match function   
    // to compare moved obj and target where it was dropped.    
    if (target != null)     
    {       
        test_match(target, obj);    
    }   
    obj.stopDrag(); 
}

function test_match(target,obj) {   
    // test if either one of the four pairs match   
    if ( (target == box_j && obj == answer_j) ||    
        (target == box_e && obj == answer_e) ||     
        (target == box_m && obj == answer_m) ||     
        (target == box_b && obj == answer_b) ||     
        (target == box_a1 && obj == answer_a1) ||   
        (target == box_t && obj == answer_t) ||     
        (target == box_a2 && obj == answer_a2) ||   
        (target == box_n && obj == answer_n) )  
    { // we got a hit       
        hits = hits+1;      
        textField.text = "Yes ! You got one !";         
        // make the object transparent      
        obj.alpha = 0.5;        
        // kill its event listeners - object can't be moved anymore         
        obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);                  
        obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);       
        // Test if we are done      
        if (hits == 8) {            
            textField.text = "Made it !!"; 
        } 
    } else {        
            textField.text = "Missed :(";
    } 
}

box_j - box_n 是应作为可拖动对象的目标的对象。

但是,由于某些未知原因,上述代码无法正常工作。如果您知道如何解决,请告知。

所有对象均为“电影剪辑”类型。

4

1 回答 1

0

只需更改 mouseUpHandler 函数中的顺序即可。

当这不起作用时,在引用 dropTarget 之前停止拖动,您应该将 .parent 添加到 dropTarget:

function mouseUpHandler(evt:MouseEvent):void {  
    var obj = evt.target;   

    obj.stopDrag(); 
    // obj.dropTarget will give us the reference to the shape of    
    // the object over which we dropped the circle.   
    var target = obj.dropTarget;  
    //var target = obj.dropTarget.parent; 

    // If the target object exists the we ask the test_match function   
    // to compare moved obj and target where it was dropped.    
    if (target != null) { 
        test_match(target, obj); 
    } 
}

编辑:

.parent这就是为什么你有时必须使用:

当您以创作模式而不是通过 AS 将目标放在舞台上时,dropTarget 属性将引用 Shape。要获取包含 Shape 的 MovieClip 或 Sprite 的实例,您需要使用 parent 参数。

于 2012-07-14T18:22:59.263 回答