A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: urmhhh... update a variable on the fly? ^^

  1. #1
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601

    urmhhh... update a variable on the fly? ^^

    well, i was just wondering if it's possible to update a variable without using additional code?

    here's the situation: i have a large numbers of values (variables) included in an array:

    Code:
    // LOAD EVENT
    strenght=10
    magic=20
    life=50
    whatever = xxx
    //
    char["power"] = [stenght, magic, life, whatever]
    imagine now i hit some enemy using my strenght value:
    Code:
    // enemy looses life
    _root.enemy.life -= char["power"][0]
    here, the enemy will lose 10 points.

    NOW, what if the strenght value was changed during the game?
    ex: strenght +=10
    if i check again with the char["power"][0], the value will still be 10, because it was calculated in a load event.

    and my question is: is there a way to update that variable into the array without launching a function that reload and recalculate the entire array? (they are more complex in my game)

    maybe something more or less magical, like char["power"][0].update()? ^^

    note; of course, i can fix it by using some if statments, or recalculate the array with a function, but i was just wondering if such a small code exists.

    thanks...

  2. #2
    Senior Member
    Join Date
    Aug 2001
    Posts
    101
    this probably isnt much help, but why store the data in an array?

    why not just say '_root.enemy.life -= strength'

    then u can just update strength when you want it to be more or less and ignore the array?

    if u do want to store the data in an array for what ever reason

    then, instead of setting the array as

    Code:
    char["power"] = [stenght, magic, life, whatever]
    set it as

    Code:
    char["power"] = ["stenght", "magic", "life", "whatever"]
    then use

    Code:
    _root.enemy.life -= this[char["power"][0]]
    but.... theres no benefit to using that over '_root.enemy.life -= strength' anyhow
    Last edited by PercyPea; 07-04-2003 at 05:45 PM.

  3. #3
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    you could make the values objects and then make the array "point" to the object rather than the value;
    Code:
    // LOAD EVENT
    strenght= { v: 10 };
    magic= { v: 20 };
    life= { v: 50 };
    whatever = { v: 76 };
    char["power"] = [stenght, magic, life, whatever]
    Now the elements in the array will "point" to the strentgh, magic, life and whatever objects. when you want to reference the actual value you do it like this;
    Code:
    // enemy looses life
    _root.enemy.life -= char["power"][0].v;
    hope this helps
    blink
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  4. #4
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    thanks people
    but why store the data in an array? why not just say '_root.enemy.life -= strength'
    well yes, of course, but my example was a little bit dumb, sorry... in fact the arrays in my game are a lot more complex; i'm working with my fighting game now, and the arrays i wrote contain all the possible moves and chgaracteristics for each character: keys manipulations, strenght points, defense points, frame that have to be played... etc. in these kind of arrays, the changing elements where the keys presses, because they are different when the character faces left or right... the backward key becomes the forward key for example, and if the keys are stored in an array in a load event, then...
    now i don't want to write something like '_root.enemy.life -= strength', simply because i want to have a clean and "universal" code, usable for every character without rewriting all the moves manipulations each time; the more it's automated, the more i'll have the opportunity to add fighters probably :-)

    blinkOk
    thanks a lot, it's surely the shorter solution.
    > so no little magic code like update() or something ^^.
    note, this one may be useful in the next flash instalment, i think.

  5. #5
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    just remember it's a pointer so if you change char["power"][0].v you will also be changing strength and is you change strength you will also be changing char["power"][0].v.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

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