A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Complex Array Question

  1. #1
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124

    Complex Array Question

    I would like to set up a table in flash to refer back to when a player encounters a monster in my game. The table will include all of the different monsters stats. It would be something very similar to this:

    Name | MaxHealth | MaxDefense | MaxAttack

    Gremlin | 2 | 5 | 1

    Dog | 5 | 7 | 3

    Ghoul | 8 | 9 | 6


    I am not sure if this is possible, but can I input this data into a complex array so that if I pull a random number, like 1, I can pull the name, maxHealth, ect. from the row containing the dog data? I understand how to set up simple arrays but I have trouble building more complex ones and then accessing the info from them. I would appreciate any help anyone can offer.
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Arrays, my favourites

    Ok, you need two-dimensional array for this. No, its not something out of other dimension, its only array with array as every element. Confused? Lets see.

    Normal, simple array that normal people can make:
    myArray=["a", "b", "c", "d"];

    Thats was easy. You can get value of first element with myArray[0], which is "a", second element myArray[1] has value "b", and so on.

    Now the clever part!
    What is we dont put "a", "b" and "c" into array, but we put other arrays there? Yes, we can do that. Here, lets make some arrays:

    a=["a1", "a2", "a3"];
    b=["b1", "b2", "b3"];
    c=["c1", "c2", "c3"];
    myArray=[a, b, c];

    Now we have declared array myArray and each element in there is also an array. So, the value of first element myArray[0] is a and a is array of ["a1", "a2", "a3"], second element has value ["b1", "b2", "b3"]. If you write

    myVar=myArray[2];
    then myVar gets value ["c1", "c2", "c3"].

    Ok, so what, you might ask. We dont have to stop here. If you write:
    myVar=myArray[2][0];
    then it gets value of first element of third element in myArray "c1".

    Lets practise more:
    myVar=myArray[0][1];
    takes first element of myArray (a) and second element from that ("a2".

    myVar=myArray[1][0];
    gets value "b1"

    myVar=myArray[1][1];
    gets value "b2"

    myVar=myArray[1][2];
    gets value "b3"

    You get the picture?
    Back to your question.
    Make the array:

    monsters = [
    ["gremlin", 2, 5, 1],
    ["dog", 5, 7, 3],
    ["ghoul", 8, 9, 6]
    ];

    Pick random enemy
    ranEnemy=random(monsters.length);

    name of randomly picked enemy is:
    name=[ranEnemy][0];

    max health of randomly picked enemy is:
    maxHealth=[ranEnemy][1];


    Hope this helped

  3. #3
    ( x x ) ( x x ) ( x x ) xxblindchildxx's Avatar
    Join Date
    Aug 2003
    Location
    London
    Posts
    282
    god i love it when u come on to flashkit and find someone written an essay about the question you were just about to ask.
    thanx for the indirect help!
    bLIndchILd
    Ross McDowell | WannabeDesigner |
    T: 020 7916 8269 | E: blindchild17@hotmail.com

  4. #4
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    Thanks, I now have something to read on the subway ride home, thank god the power is back in the Big Apple. From what I did see, it looks like you explained exactly what I was looking for... I always get confused when you create arrays and then place those arrays inside of arrays. Thanks again!
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  5. #5
    Infinite Loop grvdgr's Avatar
    Join Date
    Feb 2001
    Posts
    610
    Thanks Tonypa,
    This seems to be what I have been trying to understand also!

    Regards
    ~GD~
    Operator Error

  6. #6
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    Originally posted by xxblindchildxx
    god i love it when u come on to flashkit and find someone written an essay about the question you were just about to ask.
    thanx for the indirect help!
    bLIndchILd
    ^_^

  7. #7
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    another method of storing your information allows you to name each element. this can make your code a bit more readable and you don't have to remember the which array element contains a particular value.
    using an array of objects you can name each element;
    for your table;
    Name | MaxHealth | MaxDefense | MaxAttack
    Gremlin | 2 | 5 | 1
    Dog | 5 | 7 | 3
    Ghoul | 8 | 9 | 6
    the array of objects would look like;
    Code:
    monsters = [
    {name: "Gremlin", maxhealth: 2, maxdefense: 5, maxattack: 1 },
    {name: "Dog",     maxhealth: 5, maxdefense: 7, maxattack: 3 },
    {name: "Ghoul",   maxhealth: 8, maxdefense: 9, maxattack: 6 }
    ];
    Now to reference the "Dog" values you would code;
    Code:
    dogName = monsters[2].name;
    dogDefense = monsters[2].maxdefense;
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  8. #8
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Heck, while we're at it, let's get rid of numbers altogether!

    code:

    var monsters = {};

    monsters["Gremlin"] = { maxhealth: 2, maxdefense: 5, maxattack: 1 };
    monsters["Dog"] = { maxhealth: 5, maxdefense: 7, maxattack: 3 };
    monsters["Ghoul"] = { maxhealth: 8, maxdefense: 9, maxattack: 6 };

    trace(monsters["dog"].maxhealth);



    OK, technically this is an "2D object", not a 2D array, i cheated a little. But you can access things through the same [ ] array opperators, as in the trace at the bottom. Then you don't have to keep looking up what thing is in array slot X
    jonmack
    flash racer blog - advanced arcade racer development blog

  9. #9
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    Very interesting. I need to think about this, large chunks of data confuse me. Still trying to learn about objects and all the other amazing ways of storing and manipulating data. xmcnuggetx has been trying to explain it to me but you know us silly New Yorkers. I just want to punch things, not read about them. So after I stop hitting my computer, I will try out all of your examples. Thanks again... You know I learned how to use flash in version 3/4 and I have not been able to keep up since. I hope flash 7 AS is totally new so I can never get good at this stuff!
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  10. #10
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    jonmack: perfect solution
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  11. #11
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    jonmack, if I do it your way, how can I pull the data with a random number? The other solution is a little friendlier to my needs...
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  12. #12
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    jonmack's solution is designed for more direct access. you need to decide which way the data will be accessed. if you are mostly going to access the data directly and occasionally randomly then mabey have an array with just the names in it. ie;
    Code:
    mName = [ "Gremlin", "Dog", "Ghoul" ];
    then you can access jonmack's objects randomly by;
    Code:
    monsters[mName[random(mName.length)]].maxhealth;
    if you are going to always acces the data randomly then mabey my array might be a better solution;
    Code:
    monsters[random(monsters.length)].maxhealth;
    now if you want to randomly access the monsters AND randomly access the values then tonypa's method is the best. ie;
    Code:
    value = monsters[random(monsters.length)][random(monsters[0].length)];
    // this will randomly return either maxhealth, maxdefense or maxattack 
    // for a randomly selected monster
    really the difference would only be performance. i must say jonmack's method documents the structure much better so if you come back to the code at a later date it's much easier to read and understand.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  13. #13
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    jonmack's solution is going to be the best way I think. After posting my reply I realized if I just placed all the monster's names in an array I could randomly access them. I will need to probably access them by name at times, but as the game stands now it is going to be random. I am just building a test engine right now to see if I can really do this. I am working hard on making a playable demo for tonight. A lot of it needs to be rewritten into Objects and arrays so I doubt I will release the source code. I still program a little backwards since I was self-taught in flash 4. That is why I ask these questions and get suck great response. Many thanks again...
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  14. #14
    FK's official coffee addict gasbag15's Avatar
    Join Date
    Aug 2001
    Location
    Melbourne
    Posts
    1,867
    Where is afew good exercises to get comfortable with making arrays?
    Just some basic ones so I can have arrays down solid and be able to recall how to use them whenever needed.

  15. #15
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Originally posted by gasbag15
    Where is afew good exercises to get comfortable with making arrays?
    Just some basic ones so I can have arrays down solid and be able to recall how to use them whenever needed.
    Maybe this helps:
    http://actionscript-toolbox.com/arrayobject.php

  16. #16
    Infinite Loop grvdgr's Avatar
    Join Date
    Feb 2001
    Posts
    610
    Thank you! Thank You! Thank You!
    I sooo need to learn this and I have been struggleing with it for some time. Excellent thread and great link for learning!

    Regards
    ~GD~
    Operator Error

  17. #17
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Hi all.

    I don't think my solution is "perfect", but thanks for the love BlinkOk . I just thought i'd show another way on top of Blink's. There's always more than one solution. But i forgot about the random number things. Blink's other post about the pros/cons of each method is wise. If you need things from random numbers, then you'll have to have numbers in there somewhere! But if it's pure data then maybe mine is the best to go. It's just if you have a lot and you need a specific one ( not randomly ) then it's much easier than trying to remember that "green slimey monster 57" is in slot 93

    P.S. Nice link tonypa! Not seen that for years, good to remember it.
    jonmack
    flash racer blog - advanced arcade racer development blog

  18. #18
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    Great link, thanks... The function that calls up a fight also has an input value. If the value is Random a monster is pulled based on its order in the list/array or whatever. If there is a name in the value like “Big Ugly NY Rat”, then I would need to pull that monster’s stats. Could I fudge it by just adding an ID number to each monster and just call that number when it is random? Just a thought, this way it would work like the unique ID number used in databases?
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

  19. #19
    .fla vont's Avatar
    Join Date
    Oct 2001
    Posts
    473
    Thanks alot everyone for this great thread, now I can easly understand arrays and use them in a better way .. Thanks again.

    one more thing, good question virtuoso18

  20. #20
    Senior Member
    Join Date
    Mar 2000
    Location
    New York, New York
    Posts
    124
    Thanks, the more complex my games get, the more "good" questions I will need to ask.
    -Jesse Freeman
    http://www.gamecook.com
    http://www.residentraver.com
    http://www.iliketodream.com
    http://www.teamrally.com

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