我正在尝试在 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 是应作为可拖动对象的目标的对象。
但是,由于某些未知原因,上述代码无法正常工作。如果您知道如何解决,请告知。
所有对象均为“电影剪辑”类型。