我正在尝试在ActionScript中创建一个函数,当一个drag-gable对象被放在另一个对象上时,该函数将触发一个事件。
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是将成为可拖动对象的目标的对象。
但是,由于某些未知原因,上述代码将无效。如果您知道如何解决,请告知。
所有对象都处于“影片剪辑”类型。
答案 0 :(得分: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。至 获取包含的MovieClip或Sprite的实例 您需要使用父参数的形状。