-
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
-
Senior Member
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
-
( x x ) ( x x ) ( x x )
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
-
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
-
Infinite Loop
Thanks Tonypa,
This seems to be what I have been trying to understand also!
Regards
~GD~
-
DOT-INVADER
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
^_^
-
ism
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
-
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
-
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
-
ism
jonmack: perfect solution
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
-
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
-
ism
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
-
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
-
FK's official coffee addict
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.
-
Senior Member
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
-
Infinite Loop
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~
-
-
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
-
.fla
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
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|