Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

use event function for multiple events

Contributor ,
Apr 23, 2013 Apr 23, 2013

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?

TOPICS
ActionScript
553
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 23, 2013 Apr 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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 23, 2013 Apr 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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 23, 2013 Apr 23, 2013
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines