PDA

View Full Version : problem with removeEventListener



vedran
October 6th, 2008, 10:11 AM
i'm having problem with removing event listener from movieClip
can someone look at the code please... im in the proces of migration from as2-as3

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
//-------------------------------------------------------------------------------
// blueDot
function createGrid():void {
var gridHolder:MovieClip = new MovieClip();
gridHolder.x = 20;
gridHolder.y = 20;

var gridMovies:Array = new Array();

var xLoopNum:int = 2;
var yLoopNum:int = 2;
for (var j:int = 0; j<yLoopNum; j++) {
for (var i:int = 0; i<xLoopNum; i++) {
var tempDot:MovieClip = new MovieClip();
tempDot.addChild(new blueDot());
tempDot.name = "dot_"+i;
tempDot.x = i*25;
tempDot.y = j*25;
tempDot.rotationInterval = Math.random()*4+1;

tempDot.addEventListener(MouseEvent.CLICK,handleDo tClick);
tempDot.addEventListener(Event.ENTER_FRAME, handleDotRotation);

gridHolder.addChild(tempDot);
gridMovies.push(tempDot);
}
}
stage.addChild(gridHolder);
}
function handleDotClick(evt:MouseEvent):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.removeEventListener(Event.ENTER_FRAME, handleDotRotation);
//evt.target.parent.removeChild(evt.target);
}
function handleDotRotation(evt:Event):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.rotation += tempDot.rotationInterval;
}
//-------------------------------------------------------------------------------
createGrid();

vedran
October 6th, 2008, 10:26 AM
ActionScript Code:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
//-------------------------------------------------------------------------------
// blueDot
function createGrid():void {
var gridHolder:MovieClip = new MovieClip();
gridHolder.x = 20;
gridHolder.y = 20;

var gridMovies:Array = new Array();

var xLoopNum:int = 2;
var yLoopNum:int = 2;
for (var j:int = 0; j<yLoopNum; j++) {
for (var i:int = 0; i<xLoopNum; i++) {
var tempDot:MovieClip = new MovieClip();
tempDot.addChild(new blueDot());
tempDot.name = "dot_"+i;
tempDot.x = i*25;
tempDot.y = j*25;
tempDot.rotationInterval = Math.random()*4+1;

tempDot.addEventListener(MouseEvent.CLICK,handleDo tClick);
tempDot.addEventListener(Event.ENTER_FRAME, handleDotRotation);

gridHolder.addChild(tempDot);
gridMovies.push(tempDot);
}
}
stage.addChild(gridHolder);
}
function handleDotClick(evt:MouseEvent):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.removeEventListener(Event.ENTER_FRAME, handleDotRotation);
//evt.target.parent.removeChild(evt.target);
}
function handleDotRotation(evt:Event):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.rotation += tempDot.rotationInterval;
}
//-------------------------------------------------------------------------------
createGrid();



i'm having problem with removing event listener from movieClip
can someone look at the code please... im in the proces of migration from as2-as3

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
//-------------------------------------------------------------------------------
// blueDot
function createGrid():void {
var gridHolder:MovieClip = new MovieClip();
gridHolder.x = 20;
gridHolder.y = 20;

var gridMovies:Array = new Array();

var xLoopNum:int = 2;
var yLoopNum:int = 2;
for (var j:int = 0; j<yLoopNum; j++) {
for (var i:int = 0; i<xLoopNum; i++) {
var tempDot:MovieClip = new MovieClip();
tempDot.addChild(new blueDot());
tempDot.name = "dot_"+i;
tempDot.x = i*25;
tempDot.y = j*25;
tempDot.rotationInterval = Math.random()*4+1;

tempDot.addEventListener(MouseEvent.CLICK,handleDo tClick);
tempDot.addEventListener(Event.ENTER_FRAME, handleDotRotation);

gridHolder.addChild(tempDot);
gridMovies.push(tempDot);
}
}
stage.addChild(gridHolder);
}
function handleDotClick(evt:MouseEvent):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.removeEventListener(Event.ENTER_FRAME, handleDotRotation);
//evt.target.parent.removeChild(evt.target);
}
function handleDotRotation(evt:Event):void {
var tempDot:MovieClip = MovieClip(evt.target);
tempDot.rotation += tempDot.rotationInterval;
}
//-------------------------------------------------------------------------------
createGrid();

Smee
October 6th, 2008, 10:30 AM
Use 'evt.currentTarget' in your handleDotClick event, that way you're referencing the dot that registered the event.

vedran
October 7th, 2008, 03:27 AM
thnx, it's working
but whats the diference betwen "target" and "currentTarget"?
now i understand what "currentTarget" is refering to. In reference they say it's referencing something called "target node", whats the concept behind that?

wvxvw
October 7th, 2008, 04:39 AM
In case your event bubbles, it may be redispatched several times before it gets into your event-listener function. .target property would be the original dispatcher that created the event and .currentTarget is the immediate dispatcher that invoked the event-listener function, so target and currentTarget may be the same object as well.

vedran
October 9th, 2008, 07:25 AM
thnx for explanation!
:)