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

Remove stage listener for multiple movieclips

Community Beginner ,
Jan 27, 2014 Jan 27, 2014

I would like to completely remove personalized stage listeners, for each movieclip as soon as they are placed where they need to be, but the listeners remain every single time a new clip is dragged.

Thank you for any help!

function clickToDrag(targetClip:MovieClip):Function {

          return function(e:MouseEvent):void {

                    startingPosition[targetClip.name] = new Point(targetClip.x, targetClip.y);

                    targetClip.startDrag(false, new Rectangle(0,0,800,600));

                    setChildIndex(targetClip,numChildren - 1);

                    trace('clickToDrag function invoked\ntargetClip: ' + targetClip.name + '\startingPosition: ' + startingPosition[targetClip.name] + '\n\n');

                    stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

          }

}

/*          releaseToDrop

          @function          stopDrag for current clip, if dropped off stage, returns to recorded beginning location

          @Param                     targetClip:MovieClip

          @Param                     startPosition:int

          @Returns          event:void

*/

function releaseToDrop(targetClip:MovieClip):Function {

 

          return function(e:MouseEvent):void {

                    targetClip.stopDrag();

                    trace('releaseToDrop function invoked\ntargetClip: ' + targetClip.name + '\n\n');

                    stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

                    stage.addEventListener(Event.MOUSE_LEAVE, mouseGone);

                    function mouseGone () {

                              TweenLite.to(targetClip, .2, { x: startingPosition[targetClip.name].x });

                              TweenLite.to(targetClip, .2, { y: startingPosition[targetClip.name].y });

                              //toggle comments to ease or not ease back to startingPosition

                              //targetClip.x = startingPosition[targetClip.name].x;

                              //targetClip.y = startingPosition[targetClip.name].y;

                              stage.removeEventListener(Event.MOUSE_LEAVE, mouseGone);

                              trace('releaseToDrop function invoked\ntargetClip dragged out of bounds: ' + targetClip.name + '\n\n');

                    }

          }

}

/*          checkTarget

          @function          checks if current clip is dragged to drag1target(dock), updates boat weight and waterline, remove listeners

          @Param                     targetClip:MovieClip

          @Param                     lbsAmount:int

          @Param                     targetLocation:MovieClip

          @Returns          event:void

*/

function checkTarget(targetClip:MovieClip,lbsAmount:int,targetLocation:MovieClip):Function {

          return function(e:MouseEvent):void {

                    if (targetClip.hitTestObject(drag1target)) {

                              targetClip.x = targetClip.x;

                              targetClip.y = targetClip.y;

                              drop.play();

                              TweenLite.to(targetClip, .5, { alpha: 0, onComplete:fadein });

                              function fadein() { TweenLite.to(targetLocation, .5, { alpha: 1 }); }

                              noMC.waterlineMC.y = noMC.waterlineMC.y - 3;

                              lbs -= lbsAmount;

                              lbsTxt.htmlText = lbs + "<font size='16'>lbs</font>";

                              targetClip.buttonMode = false;

                              targetClip.mouseEnabled = false;

                              targetClip.removeEventListener(MouseEvent.MOUSE_UP, checkTarget);

                              targetClip.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);

                              /* TODO: Issue with stage listener for every clip, opportunity to handle programmatically? */

 

                              /* check to see if eventListenter is still present */

                              /*

                              if(targetClip.hasEventListener(MouseEvent.MOUSE_DOWN)) {

                                        trace(targetClip.name + ' still has MOUSE_DOWN event listener');

                              }

 

                              if(targetClip.hasEventListener(MouseEvent.MOUSE_UP)) {

                                        trace(targetClip.name + ' still has MOUSE_UP event listener');

                              }

                              */

                    } else if (borderMC.hitTestPoint(targetClip.x, targetClip.y, true)){

                              /*targetClip.y = startingPosition[targetClip.name].y;

                              targetClip.x = startingPosition[targetClip.name].x;                              */

                              TweenLite.to(targetClip, .2, { x: startingPosition[targetClip.name].x });

                              TweenLite.to(targetClip, .2, { y: startingPosition[targetClip.name].y });

                    } else {

                              /*targetClip.y = startingPosition[targetClip.name].y;

                              targetClip.x = startingPosition[targetClip.name].x;          */

                              TweenLite.to(targetClip, .2, { x: startingPosition[targetClip.name].x });

                              TweenLite.to(targetClip, .2, { y: startingPosition[targetClip.name].y });

                    }

          }

}

TOPICS
ActionScript
1.4K
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 ,
Jan 27, 2014 Jan 27, 2014

from what i see you have nested functions that will probably cause you headaches. get rid of the nesting.

The structure of any Document Class

should be:

1.define your vars

2.make an init function which checks for stage availablibility

3.in the init, add your eventlisteners and create/initialize your instances

Your code seems overly "complicated" for the problem its trying to solve

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
Community Beginner ,
Jan 28, 2014 Jan 28, 2014

I appreciate your reply, but I don't fully understand your advice because I

am very new at as3. I thought the code I had was pretty condensed!

Thank you

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 ,
Jan 28, 2014 Jan 28, 2014

i will try to show you a way that might help you to understand how the event-model in as3 is meant to work.

look at this code:

//with a MovieClip "DragClip" in the Library exoported for ActionScript

import flash.display.MovieClip;

import flash.events.MouseEvent;

for (var i:int = 0; i<10;i++){

    var mc:DragClip = new DragClip();

    addChild(mc);

    mc.mouseChildren = false;

    mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStart);

    mc.addEventListener(MouseEvent.MOUSE_UP, dragStop);

    addChild(mc);

}

function dragStart(e:MouseEvent):void {

    e.currentTarget.startDrag();

    trace("dragging started");

}

function dragStop(e:MouseEvent):void {

    e.currentTarget.stopDrag();

    trace("dragging stopped");

    e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, dragStart);

    e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, dragStop);

}

//it places hundred Movieclips on the stage

//makes them draggable

//but after they are dragged once

//they stay in place

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
Community Beginner ,
Jan 28, 2014 Jan 28, 2014

That's handy, but what if I have 22 different movieclips that need to be

dragged and dropped?

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 ,
Jan 28, 2014 Jan 28, 2014

you can push any kind of Object into an array. Then you can loop through the array and add your eventlisteners.

Suppose you have your 22 MovieClips on stage: You don`t even have to push them manually into the array, but can Actionscript let do that for you:

var mcArray:Array= new Array();

for (var i:int = 0; i<stage.numChildren;i++){

   if(getChildAt(i) is MovieClip){

     mcArray.push(getChildAt(i));

}

}

//then loop through the array and apply your eventlisteners

for (var j:int = 0; j<mcArray.length;j++){

  mcArray.addEventListener.....

}

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
Community Beginner ,
Jan 29, 2014 Jan 29, 2014

I think I understand that code a little bit! It seems great. Is

stage.numChildren saying that array spans from 0 - the amount of clips on

stage? Because I have 22 draggable objects on stage, plus, they each have a

duplicate, and there is one drop target. So theres 45 objects on stage...

Also how do I pass parameters through for each specific item?

mcArray.addEventListener(MouseEvent.MOUSE_UP,checkTarget(draggable

object, weight of specific item, drop positioning));

Thank you for your help!

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 ,
Jan 29, 2014 Jan 29, 2014

//this should work

mcArray.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent) : void { checkTarget(draggableObject, weight_of_specific_item, drop_positioning) });

function checkTarget(event:MouseEvent ,_dO:MovieClip,_weight:Number, _pos:Point 😞void {
//do sth here, depending on what arguments are received

}

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
Community Beginner ,
Jan 29, 2014 Jan 29, 2014

I appreciate your patience with my limitations. Just to be clear,

mcArray.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent) :

void { checkTarget(draggableObject, weight_of_specific_item,

drop_positioning) });

That line is only needed once? If so, how do I put 22 different parameters

in that one line? Each listener looks like this

drag21.addEventListener(MouseEvent.MOUSE_UP,checkTarget(drag21,16, drop21));

drag22.addEventListener(MouseEvent.MOUSE_UP,checkTarget(drag22, 5, drop22));

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 ,
Jan 29, 2014 Jan 29, 2014

it would be much easier for you if you just would just store your data inside your MovieClips. If you didn`t know: you can make up dynamic properties for any MovieClip like so:

drag21.weight = 16;

drag21.dropTarget = drop21;

etc.

and later you use that info in your MouseListener

so when letting go of the Mouse over the Mc drag21

e.currentTarget.weight will give you 16

e.currentTarget.dropTarget will give you drop21 etc.

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
Community Beginner ,
Jan 29, 2014 Jan 29, 2014
LATEST

Oh alright that seems easy enough, I manually store each mc's unique

information. Thank you very much for helping me! I really do want to learn

how to code efficiently!

Thank you!

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