all groups > flash actionscript > june 2004 >
You're in the

flash actionscript

group:

actionscript 2.0 - 'this' scope issue



actionscript 2.0 - 'this' scope issue pwp69
6/18/2004 10:24:49 PM
flash actionscript: Can some one tell me why I have to wrap variables normally scoped with 'this'
with 'with (this)' to get them to work. (see attached code snippet.) Is this a
bug or am I doing something wrong?

public function moveTo(xPos:Number,yPos:Number):Void {

// This works....
with (this) {
_x = xPos;
_y = yPos;
}

// This doesn't work....
this._x = xPos;
this._y = yPos

}
Re: actionscript 2.0 - 'this' scope issue MechaFlasher
6/18/2004 11:48:16 PM
Re: actionscript 2.0 - 'this' scope issue pwp69
6/19/2004 1:20:52 AM
I've attached the class file for your consideration....

// Selector Container Class
class SelectorContainer {
// declare variables
var selectorCount:Number;

// Constructor
public function SelectorContainer() {
trace("SC : " + this);
}

public function init():Void {
}

public function isVisible(tf:Boolean):Boolean {
with (this) { return _visible; }
}

public function setVisible(tf:Boolean):Void {
with (this) {
_visible = tf;
}
}

public function newSelector():Void {
}

public function moveTo(xPos:Number,yPos:Number):Void {
with (this) {
_x = xPos;
_y = yPos;
}
}

public function setTitle(title:String):Void {
with (this) {
selectorContainerTitle_txt.text = title;
}
}
}
Re: actionscript 2.0 - 'this' scope issue Flash Nick
6/19/2004 3:53:19 AM
try the following:

class SelectorContainer extends MovieClip {

I think you'll find that this will work...

this._y = yPos;

Otherwise your class really doesn't have _x or _y or any of the MovieClip
classes properties and methods..... don't ask me why with(this) works
though.... it shouldn't I would have thought.....

I've tried it one of my classes and this._y does what it outta.


Re: actionscript 2.0 - 'this' scope issue Grfx Guru
6/19/2004 5:04:00 AM
Hi,

I don't see why the dot syntax would not work, for example

return this._visible;

Should work fine in all cases, my only other suggestion is that there is
something outside the class causing some kind of a problem.

Regards,
Peter Witham
Re: actionscript 2.0 - 'this' scope issue Jeckyl
6/20/2004 12:20:29 AM
with (this) {
xxx = yyy
}
is quite different from
this.xxx = yyy

the with version will first will look in 'this' for variables to get / set,
and then if not found look without the with. In particular, it will do the
equivalent of this pseudo-code logic ...

var rhs;
if (this.yyy exists) {
rhs = this.yyy;
} else {
rhs = yyy;
}
if (this.xxx exists) {
this.xxx = rhs;
} else {
xxx = rhs;
}

this.xxx = yyy will explicitly look in current context for value of 'yyy',
and then forst it to be assigned to property 'xxx' of 'this' (whether 'xxx'
exists already or not);

So the main differences are:

* with version will look in 'this' for BOTH the lhs and rhs ot the
assignment, if not found will look in current context
* with version will only assign value of xxx in 'this' if it already exists,
otherwise will look in current context

Its a subtle difference, but important.

Re: actionscript 2.0 - 'this' scope issue pwp69
6/20/2004 2:56:39 PM
Thank you all for your replies... I think Jeckyl seems to have the answer,
however, I am still a confused.
Jeckyl, what do you mean by current context. By this do you mean a specific
instance of an object?

The object I create is a movieClip with a linked class. Inside my class file
I have a moveTo() method that positions the object appropriately. The
movieClip object does have a _x property so I thought I could just reference it
as this._x = xPos. I am having some difficulty understanding the difference
between using "with(this)" and just "this._property" in this case.

You replied:

this.xxx = yyy will explicitly look in current context for value of
'yyy',
and then forst it to be assigned to property 'xxx' of 'this' (whether
'xxx'
exists already or not);

I pass the "xPos" (yyy) in as a method parameter. When does it become part of
f "this"? The _x property IS a propery of the object so I presume it is in
"this". Doesn't this mean "this._x = xPos;" should work?

Another developer just assigns "this" to another variable and then uses it
instead, eg (var myThis = this; myThis._x = 250;) However, I think this is
klugey.

What am I missing?

Thanks again for the help.

Re: actionscript 2.0 - 'this' scope issue Jeckyl
6/21/2004 9:15:07 AM
when a method first starts executing, it is running in the context of the
scene or clip where you DEFINED the function NOT the context of the instance
of the class or clip that called it.

So if you have a parentClip and a childClip that is a child of the parent,
and in the parent clip you have:

trace("define method from within "+_name);
childClip.method = function () {
trace("inside the method, current context is "+_target);
trace("this = "+this);
trace("xxx = 'hello'");
xxx = 'hello';
trace("parentClip.xxx = "+_root.parentClip.xxx);
trace("childClip.xxx = "+_root.parentClip.childClip.xxx);
trace("with (this) { xxx = 'there' };");
with (this) { xxx = 'there'; };
trace("parentClip.xxx = "+_root.parentClip.xxx);
trace("childClip.xxx = "+_root.parentClip.childClip.xxx);
trace("this.xxx = 'world';");
this.xxx = 'world';
trace("parentClip.xxx = "+_root.parentClip.xxx);
trace("childClip.xxx = "+_root.parentClip.childClip.xxx);
}


and in the child clip you have

trace("calling the method...");
method();

then this is what you get when you run

define method from within parentClip
calling the method...
inside the method, current context is /parentClip
this = _level0.parentClip.childClip
xxx = 'hello'
parentClip.xxx = hello
childClip.xxx = undefined
with (this) { xxx = 'there' };
parentClip.xxx = there
childClip.xxx = undefined
this.xxx = 'world';
parentClip.xxx = there
childClip.xxx = world

note that the context when you enter the method is the PARENT clip where the
function was defined, NOT the CHILD clip that owns the function.

also note the difference in behaviour between
xxx = 'hello' << == assigned value in parent
with (this) { xxx = 'there'; } << == assigned value in parent
this.xxx = 'world' << == assign value in child

It is quite confusing, and a lot of people don't really understand why they
have to use 'this' and how it is that "this." and "with (this) {...}" are
actually quite different in effects.

AddThis Social Bookmark Button