ActionScript 3的鼠标按下
问题描述:
我怎么会不断地检查鼠标是否处于关闭状态,如果鼠标下来,它的动作,我CAL功能ActionScript 3的鼠标按下
我试图mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,fct)
但它只能调用函数的人,然后停止,我想不断地做,我该怎么做?
答
什么你问的已经问过,但我不能很容易地找到一个重复的问题,所以我会后的链接我问题,我在那里asking for help with a few niche issues。
在AS3处理拖动事件的一些合理的代码是:
stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(e:MouseEvent)
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.addEventListener(MouseEvent.DEACTIVATE, endDrag);
stage.addEventListener(Event.MOUSE_LEAVE, endDrag);
stage.addEventListener(Event.REMOVED_FROM_STAGE, stageEndDrag);
//trigger beginDrag event
}
function drag(e:MouseEvent)
{
//trigger drag event
}
function endDrag(e:Event)
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.removeEventListener(MouseEvent.DEACTIVATE, endDrag);
stage.removeEventListener(Event.MOUSE_LEAVE, endDrag);
stage.removeEventListener(Event.REMOVED_FROM_STAGE, stageEndDrag);
//trigger endDrag event
}
答
我觉得你可以简单地设置一些标志,当鼠标停下来并在鼠标启动时再次设置它。类似的东西:
private var down_:Boolean = false;
mk_mc.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
function onMouseDown(event) {
down_ = true;
mk_mc.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}
function onMouseUp(event) {
down_ = false;
mk_mc.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}
然后,只需查询down_
知道如果鼠标按下。
答
所以,你想要做的是调用一个函数,当鼠标移动和按钮是跌?
最简单的方法是在鼠标移动时调用函数(或者,如果您想在鼠标移动时调用函数,则使用Timer),并让它检查鼠标上/下设置的标志在采取任何行动之前。
var isDown:Boolean = false;
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
function onMouseDown(evt:MouseEvent):void
{
isDown = true;
}
function onMouseUp(evt:MouseEvent):void
{
isDown = false;
}
function onMouseMove(evt:MouseEvent):void
{
if(isDown) {
//party
}
}
这段代码有很多细微的问题。它通常工作得相当好,但是在某些情况下,它会失败,导致即使在鼠标被释放后,拖动也会停滞不前。 – zzzzBov