开发者

AS3: Drag and Drop button

开发者 https://www.devze.com 2023-03-26 07:29 出处:网络
I have a button with a MouseEvent.CLICK listener. The CLICK event is being triggered when the button is pushed, mouse is down whilerolled out, then released when rolled in on the button again. I do no

I have a button with a MouseEvent.CLICK listener. The CLICK event is being triggered when the button is pushed, mouse is down while rolled out, then released when rolled in on the button again. I do not want this to happen, the click event should not occur w开发者_运维问答hen the button is being dragged.

My flash file contains a large amount of buttons and click listeners, I would like to solve this problem with as little code as possible. What is the simplest solution to this problem?


you need to add event listeners and handlers when they are required and remove them when they are no longer needed. you will use your own logic for your needs, but here's an example:

button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);

function mouseDownEventHandler(evt:MouseEvent):void
{
    evt.currentTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
    evt.currentTarget.addEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
    trace("Mouse Down");
}

function mouseUpEventHandler(evt:MouseEvent):void
{
    evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
    evt.currentTarget.removeEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
    trace("Mouse Click (Mouse Up)");
}

function rollOutEventHandler(evt:MouseEvent):void
{
    evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
    evt.currentTarget.removeEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
    trace("Roll Out");
}

if you have a lot of buttons which behave the same way, you should create a custom button class of which all of your buttons would be instances.


On mouse down, record the mouse coordinates, do the same on mouse up and compare the two coordinates. If the distance is more than 10px (or whatever you want) then cancel the click (or set some boolean to false that allows the code in the click listener to run).

or

On mouse down, start recording the mouse coordinates, so you know the clip has been moved, then on mouse up, you know if the clip has been moved even if the user places the clip back on exactly the same spot.

0

精彩评论

暂无评论...
验证码 换一张
取 消