• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7911
  • Last Modified:

Remove Listener in Flash AS2

Hi Experts,

Within the code attached (playtrick function) I am adding a listener to the mouse which resets the playback head on a custom flv skin I have created.

If the function is re-run, the removeit function is called which should remove all instances of any movieclips or listeners. So we can, in effect, start over from scratch.

This all works fine except for my problem is that the mouse listener doesn't seem to be being cleared or deleted, so if I play the function a second time, a second instance of the mouse listener is created and when I press the _root.flvskin.my_seekBar I get a double jump in the flv.

So, to summarize, what I need to do is to get the removeit function to remove or delete the mouse listener.

Can anyone see what I am doing wrong???

Thanks,

Mark
//playtrick
function playtrick(whichflv) {
 
// run the removeit function to try to remove all listeners and movieclips
removeit();
 
this.attachMovie("FLVPlayback", "my_FLVPlybk", 30, {width:640, height:360, _x:20, _y:140});
this.attachMovie("flvskin", "flvskin", 32, {_x:20, _y:500});
 
my_FLVPlybk.seekBar = flvskin.my_seekBar;
my_FLVPlybk.contentPath = "test.flv";
 
 
var myListener = new Object(); 
myListener.onMouseDown = function() {
        if (_root.flvskin.my_seekBar.hitTest(_root._xmouse, _root._ymouse, true)) {
                my_FLVPlybk.playheadPercentage = _root.flvskin.my_seekBar._xmouse;
				my_FLVPlybk.play();
        }
};
Mouse.addListener(myListener);
 
 
//removeit
function removeit() {
	
	Mouse.removeListener(myListener);
	delete "_root.flvskin.myListener";
	removeMovieClip("_root.my_FLVPlybk");
	removeMovieClip("_root.flvskin");
	
}

Open in new window

0
crooksy88
Asked:
crooksy88
  • 2
  • 2
1 Solution
 
asaivanCommented:
I think the problem is that myListener.onMouseDown is already listening for the event.  You should try something along these lines, although I haven't tested it.  I'm not sure about the "down" part, it may be some other string.



function mouseDownHandler() {
        if (_root.flvskin.my_seekBar.hitTest(_root._xmouse, _root._ymouse, true)) {
                my_FLVPlybk.playheadPercentage = _root.flvskin.my_seekBar._xmouse;
                                my_FLVPlybk.play();
        }
};
Mouse.addEventListener("down",mouseDownHandler);
 
 
//removeit
function removeit() {
        
        Mouse.removeEventListener("down", mouseDownHandler);
        delete "_root.flvskin.myListener";
        removeMovieClip("_root.my_FLVPlybk");
        removeMovieClip("_root.flvskin");
        
}

Open in new window

0
 
crooksy88Author Commented:
Thanks for your reply but that doesn't seem to work.

The errors I get seem to suggest that you can't use Mouse.addEventListener.

???
0
 
asaivanCommented:
Oh, I see, it looks like a scope problem.  You're declaring your mouse listener inside the playtrick block.  You can't access it because it's a local object with a local scope.

In order to access it, you have to take it out of the playtrick block.  Try declaring it first, then assigning a value.  e.g.


var myListener = new Object(); 
 
function playtrick()
{
   myListener.onMouseDown...
}

Open in new window

0
 
crooksy88Author Commented:
Fantastic. That's sorted it.

Many thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now