PDA

View Full Version : flvPlayer.addListener - where's my scope?


daveystew
07-09-2008, 09:45 PM
Hiya,

I've got the following structure of clips / classes that I need help with, as I'm losing scope on a component "complete" event:

Timeline
|
+- introVideo (MovieClip linked to external class file IntroVideo.as)
|
+- player (FLVPLayback component)

In the class file, I have a method playIntro(), which sets up an event listener to fire when the video has finished playing, and it should then call a further method in the same class.

However, once the listener has been called, the scope is within the listening object, and NOT the class, therefore I can't call the class method!

The only way I can find round this (I'm f###ed if I can get Delegate to work) is to set a scope property on the listener object, so the further function can be referenced through that.

Surely it should be easier than this? What am I missing, or is this the standard way?

My code is below :)

Thanks for looking.
Dave

import mx.video.*;
import uk.co.davestewart.utils.ObjectUtils;

dynamic class IntroVideo extends MovieClip
{

// stage instances
private var player :MovieClip;
private var someClip :MovieClip;

// constructor
function IntroVideo()
{
// debug
trace('Constructor called!')

// play video after one frame
onEnterFrame = function(){playIntro();delete onEnterFrame;}
}

// methods
// the initial kickoff function
function playIntro():Void
{
// debug
trace('Playing intro...')

// clips
player.contentPath = 'someMovie.flv';

// set up listener and pass in the current scope
var flvListener = new Object();
flvListener.scope = this;

// set up the "complete" event
flvListener.complete = introComplete;
player.addEventListener("complete", flvListener);
}


// this function is called when video has finished playing
function introComplete(evt:Object):Void
{
// debug
trace('Intro complete.');
ObjectUtils.trace(evt, 'Event');
ObjectUtils.trace(this, 'Listener');


// IMPORTANT! This is where I'm using the passed scope
// in order to call the next function within the correct scope
this.scope.someOtherFunction();
}

// is the final function to be called - of which the call relies on having correct scope!
function someOtherFunction():Void
{
someClip._xscale = 200;
}

}

And here's my ObjectUtils.trace() output:
Constructor called!
Playing intro...
Intro complete.

---------------------------------------------------------------
- [Event] => Object
---------------------------------------------------------------

[target] => _level0.container.player
[type] => complete
[state] => stopped
[playheadTime] => 31.178
[vp] => 0

---------------------------------------------------------------

---------------------------------------------------------------
- [Listener] => Object
---------------------------------------------------------------

[complete] => [type Function]
[scope] => _level0.container // here is where I had to pass the scope in, as a property of the listener. Is this right?

---------------------------------------------------------------

xxneon
07-09-2008, 11:34 PM
flvListener.complete = mx.utils.Delegate.create(this, introComplete);
this is probably closer to what your looking for.. this will accually call the introComplete function at the scope of the class instance .. so you dont lose your class scope.

daveystew
07-10-2008, 09:21 AM
Awwwwww.... I'm kicking myself now as I've figured out why Delegate wasn't working.

I thought it must be something special as components seems to want an object with functions handed to them. But... what am I missing near the top of my class!?

The bloody import statement.

I find it really annoying when the compiler doesn't complain about this sort of stuff! That's its job FFS!! :mad:

You know if you hadn't have put the fully qualified path in your example xxneon, I would have missed that.

Anyway - the final code (WITHOUT an intermediate listener object, which is a pain in the ass in my view) pointing directly to a class method, is now:

player.addEventListener("complete", Delegate.create(this, introComplete));

Thanks loads for the fresh eyes,
Cheers,
Dave