Skip to main content
Inspiring
April 15, 2006
Answered

listeners, variables and properties

  • April 15, 2006
  • 17 replies
  • 1457 views
Hi.

All code is from the 1st frame of the main timeline.
In my .swf I attached several instances of a MC to the main timeline:

for (k = 1; k <= nTotalMCs ; k++) {
attachMovie("id1", "mc"+k, _root.getNextHighestDepth(), this["o"+k]);
trace (this["mc"+k]); //returns _level0.mc1, _level0.mc2,..., _level0.mc3
}

I have a programatic tween and I have added listeners to handle events related to it. This is one of them:

oListener01.onMotionChanged = function():Void {
for (j=1; j<=nTotalMCs; j++) {
trace (["mc"+j]); //returns mc1, mc2,..., mcN
trace (this["mc"+j]); //returns undefined
trace (this._parent["mc"+j]); //returns undefined
trace (_root["mc"+j]); //returns _level0.mc1, _level0.mc2,..., _level0.mcN
}
};

I need to access those MCs from the listner, but I can't use _root because the .swf this is in is going to be loaded by other .swfs, so I need to use relative paths. Can anyone tell me what I'm doing wrong and how to get the correct relative path? Thank you in advance.
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Peter Lorent
David, yes, my problem is solved, what I meant is that I'm having trouble fully understanding why it's solved, namely why the argument on the listener's functions automatically refers to the Mc that was Tweened. That's the only reason why I haven't marked this question as answered. Thank you for your kind help :)
Might be a bit complicated what you are doing...
Just set a reference to the timeline and use that reference in your listener.onMotionChanged. Attached pseudo code. The argument you mentioned is received from the Tween Class when it broadcasts the onMotionChanged event and holds the tweened object.

17 replies

Inspiring
April 16, 2006
LuigiL,

> About using the listener on the Tween Class.
> from the Tween Class:
>
> public var addListener:Function;
> public var removeListener:Function;

Good eye. Well, that explains that. Curious, then, why the AS
Language Reference prefers the handler approach.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
April 16, 2006
People tend to forget to cleanup these types of listeners?
Inspiring
April 16, 2006
Wow, guess I was right not to mark this question as answered, all this new info has been very useful and you both provided me with good insight and solutions. Thank you!
Inspiring
April 16, 2006
About using the listener on the Tween Class.
from the Tween Class:

public var addListener:Function;
public var removeListener:Function;
Inspiring
April 15, 2006
maguskrool,

> oListener01.onMotionChanged = function():Void {

The only onMotionChanged event I see in the ActionScript Language
Reference is Tween.onMotionChanged. In the docs, the example shows an event
handler, not an event listener. With listeners, a generic Object instance
is used as a kind of proxy. Since you named your object oListener01, it
*seems* like you're using a listener ...

var oListener bject = new Object();
oListener.onMotionChanged = function() {};
oSomeOtherObject.addEventListener(oListener);

Is that what you're doing? If so, the Tween class doesn't work that
way. )Are you even using the Tween class?)

David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
April 16, 2006
David, thank you for your reply.

Yes, I'm using the Tween class. I did the following:

//Listener
var oListener01:Object = new Object();
oListener01.onMotionChanged = function():Void {
for (j=1; j<=nTotalMCs; j++) {
trace (["mc"+j]); //returns mc1, mc2,..., mcN
trace (this["mc"+j]); //returns undefined
trace (this._parent["mc"+j]); //returns undefined
trace (_root["mc"+j]); //returns _level0.mc1, _level0.mc2,..., _level0.mcN
}
};

and later:

var tw1:Tween = new Tween(....);
tw1.addListener(oListener01);

I see what you mean, after consulting the AS Language Reference, but this works. I also tried adding a oListener01.onMotionFinished = function () { trace ("motion finished"); }; and it worked, I got the correct traced message when the Tween ended. I didn't come up with these listeners myself, however, I adapted them from an example in

LOTT, Joey, REINHARDT, Robert, "Flash 8 Actionscript Bible", Wiley Publishing, p.265. The code is attached.