all groups > flash actionscript > april 2006 >
You're in the

flash actionscript

group:

listeners, variables and properties


Re: listeners, variables and properties David Stiller
4/15/2006 7:28:57 PM
flash actionscript:
maguskrool,

[quoted text, click to view]

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:Object = 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."

Re: listeners, variables and properties David Stiller
4/15/2006 9:12:41 PM
maguskrool,

[quoted text, click to view]

Well, I can't argue with your success. ;) If it works, it works. But
if you do it according to the AS Language Reference, your reference problem
is solved. The global "this" property would refer to the Tween instance,
and that instance knows which MC it manipulates.

I ran a quick copy/paste taste from the Tween.onMotionChanged event
entry. This comes directly from the docs.

import mx.transitions.Tween;
var myTween:Tween = new Tween(img1_mc, "_x",
mx.transitions.easing.Elastic.easeOut,0, Stage.width - img1_mc._width, 3,
true);

myTween.onMotionChanged = function() {
trace(this.position);
};

So right off, I know that the Tween class features a Tween.position
property, because it refers to this.position. Of course, that information
isn't new: the Tween class entry itself lists all the available properties.

To see what other properties are available, I changed that trace()
statement a bit ...

myTween.onMotionChanged = function() {
for (prop in this) {
trace(prop + ": " + this[prop];
}
};

Doing so put a bunch of useful information in the Output panel,
including a property called "obj", whose value was the clip in question.
So, from this bit of sleuthing I learned that Tween.obj refers to the movie
clip in question.

Maybe that'll give you what you're after. Or maybe you can use a
similar process to discover another refernce (or whateverObj.obj may just
work for you, too).


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

listeners, variables and properties maguskrool
4/15/2006 11:01:17 PM
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);
trace (this); //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 (); //returns mc1, mc2,..., mcN
trace (this); //returns undefined
trace (this._parent); //returns undefined
trace (_root); //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.
Re: listeners, variables and properties maguskrool
4/16/2006 12:41:36 AM
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 (); //returns mc1, mc2,..., mcN
trace (this); //returns undefined
trace (this._parent); //returns undefined
trace (_root); //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.


import mx.transitions.Tween;
import mx.transitions.easing.*;
var aClasses:Array = [Back, Bounce, Elastic, Regular, Strong];
var aEasingMethods:Array = new Array();
for(var i:Number = 0; i < aClasses.length; i++) {
aEasingMethods.push(aClasses[i].easeIn);
aEasingMethods.push(aClasses[i].easeOut);
aEasingMethods.push(aClasses[i].easeInOut);
}
var mClip:MovieClip;
var nDepth:Number;
var nX:Number = 20;
var oClips:Object = new Object();
// Each time the onMotionFinished() method is called, restart the
// tween in the reverse direction.
var oListener:Object = new Object();
oListener.onMotionFinished = function(twObject:Tween):Void {
twObject.yoyo();
};
for(var i:Number = 0; i < aEasingMethods.length; i++) {
nDepth = this.getNextHighestDepth();
mClip = this.attachMovie(?Circle?, ?mClip? + nDepth, nDepth, {_x: nX, _y: 50});
nX += mClip._width + 5;
oClips[mClip._name] = aEasingMethods[i];
mClip.onPress = function():Void {
var twMove:Tween = new Tween(this, ?_y?, oClips[this._name], 50, 350, 2, true);
// Add the listener object.
twMove.addListener(oListener);
};
}
Re: listeners, variables and properties maguskrool
4/16/2006 4:30:17 AM
Thank you, David, but unfortunately I'm still having some trouble. What follows
is what I discovered through trial and error and using your helpful insights,
though I'm not 100% sure of how or why this works. Using the book I mentioned
before as reference, I added and argument to the onMotionChanged function:

var oListener01:Object = new Object();
oListener01.onMotionChanged = function(twMyTween):Void {
trace (this); // returns
trace (valueOf(this)); // returns _level0
trace (twMyTween); // returns
trace (twMyTween.obj); // returns _level0.tw1, the tween to which this
listener belongs to
}

Now, having a path to the tween (twMyTween.obj), I can reach the _root with a
relative path (twMyTween.obj._parent). I used

var oListener01:Object = new Object();
oListener01.onMotionChanged = function(twMyTween):Void {
for (k = 1; k <= nTotalMCs ; k++) {
trace (twMyTween.obj._parent); //returns _level0.mc1,
_level0.mc2,...,_level0.mcN
}

What I still don't get is why the argument twMyTween automatically refers to
the Tween to which the listener is attached to, but my guess is that it's
explained in the documentation (if it exists, since there was none for
listeners for Tweens, and they work, or then I'm just so confused about so much
right now that I don't have a clue).

Anyway, if someone can shed some light on the issues that weren't solved or
provide a better solution, I'd greatly appreciate it. Thank you.
Re: listeners, variables and properties David Stiller
4/16/2006 9:42:39 AM
maguskrool,

[quoted text, click to view]

Doesn't that solve your trouble? Maybe I don't fully understand your
question -- could very well be. ;)

In your original post, you said ...

[quoted text, click to view]

.... and twMyTween.obj gives you that, right? Unless you're wondering how to
path to the *other* movie clips from a given clip's event handler/listener.
If that's it, it looks like twMyTween.obj._parent would be your path --
assuming all these movie clips share the same parent.

// From mc1's point of view ...
twMyTween.obj.mc2
twMyTween.obj.mc3
twMyTween.obj.mc3
// etc.

[quoted text, click to view]

I'm afraid I can't answer that off the top of my head, but I've never
considered using listeners with the Tween class. Clearly, it works, but I'd
have to do some experimentation myself to satisfy myself as to why.

I have tons of respect for Joey Lott, so I'm definitely curious now
about t his alternative approach to Tween events. The ActionScript Language
Reference is certainly my first stop in researching any object, but it's not
my *only* stop.

[quoted text, click to view]

I'll do my best to answer your issue, but I'll have to play with it for
a while.


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

Re: listeners, variables and properties maguskrool
4/16/2006 3:53:12 PM
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 :)
Re: listeners, variables and properties David Stiller
4/16/2006 5:36:53 PM
LuigiL,

[quoted text, click to view]

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."

Re: listeners, variables and properties David Stiller
4/16/2006 7:39:12 PM
LuigiL,

[quoted text, click to view]

I do not doubt you, but I'm curious where you heard that. Maybe a
technote, somewhere? Certain events, like MovieClip.onRelease, seem to work
well enough with event handlers, for example -- and certainly, event
handlers are used in the docs for such entries.


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

Re: listeners, variables and properties LuigiL
4/16/2006 8:15:51 PM
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 by the Tween Class when it broadcasts the onMotionChanged event and
holds the tweened object.



var myListener:Object=new Object();
// add a reference to the current timeline
myListener.owner=this;
myListener.onMotionChanged=function():Void{
for (var j=1; j<=nTotalMCs; j++) {
trace (this.owner["mc"+j]); // keyword this refers to myListener
}
}
Re: listeners, variables and properties LuigiL
4/16/2006 8:31:39 PM
About using the listener on the Tween Class.
from the Tween Class:

public var addListener:Function;
Re: listeners, variables and properties LuigiL
4/16/2006 9:46:15 PM
Re: listeners, variables and properties maguskrool
4/16/2006 10:06:40 PM
Re: listeners, variables and properties LuigiL
4/16/2006 10:10:51 PM
You're welcome.
Re: listeners, variables and properties LuigiL
4/17/2006 12:00:00 AM
I've read it in several books. One of them is Essential ActionScript 2.0 by Colin Moock.
Re: listeners, variables and properties David Stiller
4/17/2006 8:03:04 PM
[quoted text, click to view]

Thanks, LuigiL! If it's in Essential ActionScript 2.0, then it likely
does relate to custom objects -- in fact, in that light, your observation
almost rings a bell for me. I'll have to check the book again.


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

Re: listeners, variables and properties LuigiL
4/18/2006 12:00:00 AM
[quoted text, click to view]
Re: listeners, variables and properties David Stiller
4/18/2006 7:23:13 PM
Oh, it is! But I'll have to open it and read it again. ;)


[quoted text, click to view]

AddThis Social Bookmark Button