|
-
[RESOLVED] my variable isnt registering as a variable
my variable is "objectHeight"+i
depending on what i is the variable becomes
objectHeight1
objectHeight2
objectHeight3
etc
the problem is that its not registering as a variable
edit:
by variable becomes i don't mean what its equal to but the name of the variable.
Ignore the top and the title. I'm gonna try to explain it the best i can now.
Lets say im trying to make a "targeting" system for a rpg game or something.
PHP Code:
if (target == 1){
enemy_hp1 -= 20
}
if (target == 2){
enemy_hp2 -= 20
}
instead of doing that for every enemy i want to do something like this. (obvoiusly it's not correct)
PHP Code:
["enemy_hp"+target] -=20
Last edited by HanTV; 12-30-2009 at 09:07 PM.
Reason: being more clear
-
How you you know its not registering?
Have you define it properly? eg: public var objectHeight:string
Have you done a trace before you add the +i to see if the problem isnt with defining the varible but with code that uses the varible?
-
 Originally Posted by amcotterill
How you you know its not registering?
Have you define it properly? eg: public var objectHeight:string
Have you done a trace before you add the +i to see if the problem isnt with defining the varible but with code that uses the varible?
i set objectHeight1 to a number
set i to 1
set another variable to be equal to "objectHeight"+i
it came out as objectHeight1 instead of the number i picked earlier.
-
-
Flash/Flex Developer
You are using quotation which is assigning the variable with the string instead of the value. It should look something like this
PHP Code:
var objectHeight:Number = 1; var anotherVar:Number;
anotherVar = objectHeight + 1; trace (anotherVar) // 2;
If you place quotes around objectHeight, it will treat it as string instead of a number. That is one reason I declare the variable and define its type, this way I will get a mismatch error if I try to shove a string in a variable defined for a number.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
 Originally Posted by samac1068
You are using quotation which is assigning the variable with the string instead of the value. It should look something like this
PHP Code:
var objectHeight:Number = 1; var anotherVar:Number;
anotherVar = objectHeight + 1; trace (anotherVar) // 2;
If you place quotes around objectHeight, it will treat it as string instead of a number. That is one reason I declare the variable and define its type, this way I will get a mismatch error if I try to shove a string in a variable defined for a number.
im not trying to add 2 vars together. i dont want objectHeight +1 i want it to be objectHeight1. i want the number not to be added to the variable but added to its name so it becomes a different variable.
Last edited by HanTV; 12-05-2009 at 02:27 AM.
Reason: trying to be more clear
-
-
-
Flash/Flex Developer
Okay, you can use the eval or [] commands. Depending on the version of actionscript, the [] should work in both 2.0 and 3.0. Here is an example
eval("example1") = "Hello"
or
["Example1"] = "Hello"
As long as the variables have been declared you can reference them using either of these methods. If you are using actionscript 2.0, then it won't be as much of a issue to declare the variables prior.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
Flash/Flex Developer
Did you try my suggestion above? That should do what you want?
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
 Originally Posted by samac1068
Did you try my suggestion above? That should do what you want?
It's coming up with errors
-
Assume you having 20 target MovieClips(target1, target2 ,...,target20) and you have to set position of all in a fetch. Using the "this" operator you can do this
for(var i=0;i<=20;i++){
this['target'+i].y=i*20;
}
as well as you can set the variable like the same
for(var i=0;i<=20;i++){
this['variable'+i] = 20;
}
-
wow... I fixed it
PHP Code:
example2 = 50;
i = 2;
this["example"+i] -= 20;
i had to put "this"...... I was trying to find out what was wrong for so long....
-
 Originally Posted by Johnny2528
Assume you having 20 target MovieClips(target1, target2 ,...,target20) and you have to set position of all in a fetch. Using the "this" operator you can do this
for(var i=0;i<=20;i++){
this['target'+i].y=i*20;
}
as well as you can set the variable like the same
for(var i=0;i<=20;i++){
this['variable'+i] = 20;
}
i didnt see your post when i posted earlier. thanks anyway
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
|