拖放功能不起作用 - Flash
Posted
技术标签:
【中文标题】拖放功能不起作用 - Flash【英文标题】:Drag & Drop Function Not Working - Flash 【发布时间】:2012-07-14 05:23:41 【问题描述】:我正在尝试在 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 是应作为可拖动对象的目标的对象。
但是,由于某些未知原因,上述代码无法运行。如果您知道如何解决,请告知。
所有对象都是“电影剪辑”类型。
【问题讨论】:
什么不起作用?出现错误?您不能拖动影片剪辑,或者它不会停止拖动或 test_match?但我想我知道出了什么问题。 【参考方案1】:只需更改 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。到 获取包含 形状你需要使用父参数。
【讨论】:
谢谢回答,我试过了,还是不行,这是错误信息之一 btwTypeError: Error #1009: Cannot access a property or method of a null object reference. at test_fla::MainTimeline/test_match() at test_fla::MainTimeline/mouseUpHandler()
不要在 test_match() 函数中使用 target == box_e
,而是使用 target.name:target.name == "box_e"
。也许这会有所帮助。
只有在调用处理程序时 textField
变量不存在时才会发生空引用错误。
@ShogunArts.de 在不存在的对象上调用字段时会发生空引用错误,即target == box_e
不能是罪魁祸首 - 如果其中一个为空,则表达式仍将有效:null== box_e
或 target == null
完全合法。它必须是对textField.text
的调用。唯一的其他解释是,如果所有target
、obj
和命名实例都是null
——在这种情况下,对obj.alpha
等的调用会导致这样的错误。
是的,你说得对,我没有过多考虑我的评论,我只是认为当 MainTimeline 发生错误时,文本字段必须在舞台上,而不是用脚本生成.所以我将文本字段作为错误源关闭了。以上是关于拖放功能不起作用 - Flash的主要内容,如果未能解决你的问题,请参考以下文章