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?
---------------------------------------------------------------
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?
---------------------------------------------------------------