-
String to variable name - in a function?
Hi guys,
I've read (and tried) that this works -
Code:
var test1:string = "hello"
trace(this["test" + 1])
BUT - when i try this in a function - with a local variable, it wont work
Code:
function myFunction():void{
var test1:string = "hello"
trace(this["test" + 1])
}
I think it's because the local variable cannot be found by "this"
Can someone please help how to get the variable name in the function - using a string!
Thankyou!
-
Senior Member
I'ts usually easier to use arrays instead of variables created dynamically.
I'm not sure why your code dosn't work. However, I got this code to work:
PHP Code:
function myFunction():void
{
var test1:String = new String
this.test1 = "hello";
trace(this["test" + "1"]);
}
myFunction ();
also see:
http://www.kirupa.com/forum/showthre...e-NAMES-in-AS3
-
Senior Member
The local variable is within the function and not on the main timeline. So just eliminate "this".
function myFunction():void
{
var test2:String = "hello";
trace(["test" + 2]);
}
- The right of the People to create Flash movies shall not be infringed. -
-
Thanks guys,
cancerinform -- that worked perfect. cheers!
Can i just update this question -- is there anyway to store the paths in strings too?
For example:
Code:
var myEG:String = 'movieclipName.textboxName';
// then do something like the following:
this[myEG].text = "hello!";
Thanks again for the help
-
Senior Member
This is not so good, because 'Object' is not a specific type cast. It is better to replace it with 'TextField'.
var myEG:Object = mc.myText;
myEG.text = "hello!";
- The right of the People to create Flash movies shall not be infringed. -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|