A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [RESOLVED] my variable isnt registering as a variable

  1. #1
    Member
    Join Date
    Oct 2009
    Posts
    30

    resolved [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

  2. #2
    Junior Member
    Join Date
    Nov 2009
    Posts
    28
    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?

  3. #3
    Member
    Join Date
    Oct 2009
    Posts
    30
    Quote Originally Posted by amcotterill View Post
    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.

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    30
    bump

  5. #5
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    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.

  6. #6
    Member
    Join Date
    Oct 2009
    Posts
    30
    Quote Originally Posted by samac1068 View Post
    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

  7. #7
    Member
    Join Date
    Oct 2009
    Posts
    30
    bump

  8. #8
    Member
    Join Date
    Oct 2009
    Posts
    30
    still no response

  9. #9
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    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.

  10. #10
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    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.

  11. #11
    Member
    Join Date
    Oct 2009
    Posts
    30
    Quote Originally Posted by samac1068 View Post
    Did you try my suggestion above? That should do what you want?
    It's coming up with errors

  12. #12
    Junior Member
    Join Date
    Dec 2009
    Posts
    11
    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;
    }

  13. #13
    Member
    Join Date
    Oct 2009
    Posts
    30
    wow... I fixed it
    PHP Code:
    example2 50;
    2;
    this["example"+i] -= 20
    i had to put "this"...... I was trying to find out what was wrong for so long....

  14. #14
    Member
    Join Date
    Oct 2009
    Posts
    30
    Quote Originally Posted by Johnny2528 View Post
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center