Copy link to clipboard
Copied
I've got several movieclip buttons on stage. For one of those buttons I've started of with:
button1.buttonMode = true;
button1.useHandCursor = true;
//
button1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
//;
function fl_ClickToDrag(event:MouseEvent):void
{
button1.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
button1.stopDrag();
}
Which works. But I would like to have that fl_ClickToDrag function excuted for all my other buttons too, so I don't have to duplicate that several times for each button. So I thought I use something like:
button1.buttonMode = true;
button1.useHandCursor = true;
button2.buttonMode = true;
button2.useHandCursor = true;
//
button1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
button2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
//;
function fl_ClickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}
But that gives an error. What's the correct way?
Copy link to clipboard
Copied
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);....
event.target.stopDrag();
the stage can`t be drag or dropped.
make sure that your draggable button is always on the highest Level (stage.numChildren-1)
and then go through all your buttons with
for(var i:int=1;i++;i<[howmanyButtonsYouHave]){
getChildByName("button"+i).addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
getChildByName("button"+i).addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
}
Copy link to clipboard
Copied
Actually I got it working by using:
button1.buttonMode = true;
button1.useHandCursor = true;
button2.buttonMode = true;
button2.useHandCursor = true;
//
button1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
button2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
//;
function fl_ClickToDrag(event:MouseEvent):void
{
event.currentTarget.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
stopDrag();
}
Copy link to clipboard
Copied
If you don`t need the info which target was dropped, then this is fine, too.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now