[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove Listener in Flash AS2

Posted on 2008-11-10
4
Medium Priority
?
7,856 Views
Last Modified: 2013-11-11
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
Comment
Question by:crooksy88
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:asaivan
ID: 22923111
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
 
LVL 13

Author Comment

by:crooksy88
ID: 22923230
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
 
LVL 9

Accepted Solution

by:
asaivan earned 2000 total points
ID: 22923689
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
 
LVL 13

Author Closing Comment

by:crooksy88
ID: 31515082
Fantastic. That's sorted it.

Many thanks for your help!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses
Course of the Month18 days, 11 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question