View Full Version : Using variables created from "this"
waffe
January 13th, 2005, 10:37 PM
I am trying to select an object and use its path as a varible in flash
c1.c2.onPress =function () {
selected_object = this
trace(selected_object)
selected_object = selected_object.substr(0, 2)
trace(selected_object)
}
The 1st trace command yields the full path, but the second trace command yields an "undefined". This problem seems to exist all over, if I use the variable from “selected_objcet = this” for any thing it does not work unless I but "" around it - this of course, does not solve the problem.
Another way to point out the problem, if I do a comparison check in flash with the variable created from "this" I get:
c1.c2.onPress =function () {
selected_object = this
selected_object == c1.c2 <------- False in flash
selected_object == "c1.c2" <------- True in flash
}
Is there a way around this problem?
senocular
January 13th, 2005, 10:43 PM
"this" is a reference. It references the object (movieclip or button). When you trace this, Flash needs a way to represent that object as text so you can get something readable in the output window. What you see in trace is only a string representation of the object itself, not the true value of the variable. If you want that string version of the object without using trace you can say
stringVariable = this.toString();
And that will assign stringVariable to the actual text response you get from tracing this or any other object.
waffe
January 14th, 2005, 02:13 AM
Thanks senocular,
but when I run the code below I get [Object Object] for the output.
c1.c2.onPress = function () {
stringVariable = this.toString();
}
What am I missing?
senocular
January 14th, 2005, 07:45 AM
ooo thats something I dont know! It seems Flash overrides toString method for movieclip tracing providing its path instead. weird.
Ok, well, as a workaround you might want to try comparing the _target property instead which represents the movieclip path (just in slash syntax - you can trace it to see how that looks).
Anywho, the point here is two things.
1) tracing is not always completely representative of the variable. Most variables are not strings but the only thing that can be traced is strings (text) so a conversion needs to be made either through toString or by Flash (apparently) for you to see a traced value. Comparing in code won't work with these traces (unless its a string you are tracing)
2) MovieClips and Button values are "objects" or, really, references to objects. As with 1, these are not strings so string functions cannot be used on them directly and to compare objects for equality you have to compare your object with another object (not a string).
If you are looking for c1 from c2 you can use clip._parent.
If you are looking for a (string) slash-syntax version of a movieclip path, use clip._target
If you are just looking for the (string) name of the movieclip, use clip._name
waffe
January 14th, 2005, 09:39 AM
Thanks for the help Senocular - I have not tried what you last posted because I have a solution to the problem. :)
c1.c2.onPress =function () {
selected_object = String(this)
trace(selected_object)
selected_object = selected_object.substr(0, 2)
trace(selected_object)
}
Just out of curiosity, what is the major difference between the command String(this) vs. this.toString() ?
senocular
January 14th, 2005, 09:46 AM
ah, that works ;)
The difference between the two is that String is a class/function that stands on its own. It will always be the same function whenever you use it. toString() is a method that is called from objects. This can change based on the object type (each type usually has its own or inherits a toString method from a parent class) or whatever toString is defined for it. So a number variable's toString would be different than an array's toString(). String, usually, pretty much invokes a toString in objects. But, as Ive seen thanks to your inquiry, that is not the case for movieclips. And in fact, I should revise my trace explanation to say trace actually traces the String(value) of a value as opposed to its toString, though normally, String() will call an object's toString (just not in the case of MovieClips).
waffe
January 14th, 2005, 10:16 AM
New question (problem) that is relative to this thread. I am trying to get the (x, y) coordinates from the variable created from selected_object = String(this).
c1.c2.onPress =function () {
selected_object = String(this)
trace(selected_object)
selected_object = selected_object.substr(0, 2)
trace(selected_object)
}
this.onEnterFrame = function() {
selected_objectX = selected_object._x
trace(selected_object)
trace(selected_objectX)
The first trace yields the c1 but the second trace yields “undefined”. I believe the problem has to do with the variable created from selected_object = String(this) is not c1 but “c1”.
senocular
January 14th, 2005, 10:26 AM
right, this goes back to the object vs string thing. Movieclips are objects so they have properties like _x which define their x locations. Strings are just text. If you want to get the object c1 from c2, thats when you use _parent. The object c1 is c2._parent. c1's x location can be found using c2._parent._x.
waffe
January 14th, 2005, 10:30 AM
Got it - Thanks for for the hook "ups" senocular!
senocular
January 14th, 2005, 10:36 AM
;)
waffe
February 3rd, 2005, 12:41 PM
Ok, ran into a new problem when using this as a variable.
this.onEnterFrame = function() {
for (i=1; i<3; i++) {
Stringpiece = eval(piece+".sub_box_"+i)
//Stringpiece = piece+".sub_box_"+i
//Stringpiece = [piece+".sub_box_"+i]
//Stringpiece = Object(piece+".sub_box_"+i)
//Stringpiece = piece+".sub_box_"+i.toString()
//Stringpiece = String(piece+".sub_box_"+i)
//numberString = String(i)
trace(Stringpiece)
if(Stringpiece.hitTest(circle_1)){
trace("hit")
}
}
};
box_1.onPress = box_2.onPress = box_3.onPress = function(){
this.startDrag()
piece = this
}
box_1.onRelease = box_2.onRelease = box_3.onRelease = function(){
this.stopDrag()
}
What I have is three box buttons on the stage each with two boxes inside of them. Now when I select a box I get its path and put it in the variable called piece, say it is box_1. Now in the loop the variable “piece” is in the eval statement along with the rest of the path like so - eval(piece+".sub_box_"+i). This works, but only with the eval statement. I would like to know is there a better way to do this – a way that does not use the eval statement. You can see some of the variations I have gone though at the top of my code, none of them work and some create false hit detects.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.