Skip to main content
Known Participant
January 27, 2014
Question

Remove stage listener for multiple movieclips

  • January 27, 2014
  • 1 reply
  • 1440 views

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

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

          @9397041                     targetClip:MovieClip

          @9397041                     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

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

          @9397041                     targetClip:MovieClip

          @9397041                     lbsAmount:int

          @9397041                     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 });

                    }

          }

}

This topic has been closed for replies.

1 reply

Inspiring
January 28, 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

yuleCoderAuthor
Known Participant
January 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

Inspiring
January 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