Skip to main content
Inspiring
April 23, 2013
Question

use event function for multiple events

  • April 23, 2013
  • 1 reply
  • 593 views

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?

This topic has been closed for replies.

1 reply

Inspiring
April 23, 2013

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);

}

jiggy1965Author
Inspiring
April 23, 2013

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();

}

Inspiring
April 23, 2013

If you don`t need the info which target was dropped, then this is fine, too.