Be a Supporter!

Arrays with multiple possibilities

  • 252 Views
  • 6 Replies
New Topic Respond to this Topic
blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Arrays with multiple possibilities 2012-10-02 21:23:18 Reply

This is gonna be a hard one to explain I think... I've got this interactive map that i'm working on in flash for a school project... all of the locations are stored in an array, with all their information, and then the user can click buttons to show certain locations... Once they click an option, the array goes through a filter to find ones that match... Each array element looks something like this:

{icon: Container.addChild(new Location("Location 1", 800, 200)), time: "day", music: "good"}

And a filter would, for example, check to see if the music type equals "good", and show it if it does.

The problem is that some things will have more than one type of music... So now my two options are to re-do this code a different way completely in a different way to make it work (which I know how to do, if I have too..). Or, ideally, find a way to just store multiple values in each category... for example, I want to be able to do something like, 'music: "good" && "crappy"' so that will show up if the filter is looking for good, as well as if it is looking for crappy.

Does that make sense? If so.. is there an easy way to do it like that, or will I have to re-do this all differently?


BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 21:57:57 Reply

You could have it as comma separated values like "good,crappy" etc. Then when it checks the value of the string, it should call the split method with the comma as the delimiter. This gives you an array of substrings which you can then match to "crappy" and all.

blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 22:01:17 Reply

At 10/2/12 09:57 PM, MSGhero wrote: You could have it as comma separated values like "good,crappy" etc. Then when it checks the value of the string, it should call the split method with the comma as the delimiter. This gives you an array of substrings which you can then match to "crappy" and all.

Wow haha, thats so easy and smart! You just saved me a ton of time... The way that I was going to do this to fix it would have been a huge pain. Thanks a billion!


BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 22:18:38 Reply

At 10/2/12 10:01 PM, blakemoso wrote: Wow haha, thats so easy and smart! You just saved me a ton of time... The way that I was going to do this to fix it would have been a huge pain. Thanks a billion!

I was only expecting a couple hundred thousand, thank you!

It super really helps to read the api. It's nice to know what the normal classes are actually capable of (like String.split()), and of course when there's something new you just learned.

blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 22:31:51 Reply

At 10/2/12 10:18 PM, MSGhero wrote:
At 10/2/12 10:01 PM, blakemoso wrote: Wow haha, thats so easy and smart! You just saved me a ton of time... The way that I was going to do this to fix it would have been a huge pain. Thanks a billion!
I was only expecting a couple hundred thousand, thank you!

It super really helps to read the api. It's nice to know what the normal classes are actually capable of (like String.split()), and of course when there's something new you just learned.

Yea, I try to read up about stuff as much as I can... I'm just too new to it all to have gotten to everything, or to remember it all... I've used split before, it just would not have ever occurred to me to use it here...

On a side note, now that I've got my array with all the possibilities in it, whats the best way to check if one certain thing is present in the array?

Like, inside the filter for the location array, I need it to either return true if the options array contains one certain thing, and return false if it does not. I know I could check through each value in the options array individually to see if they match, but It seems a little long-handed... So would making another filter for the options array just to filter it down to check for the one item. Is there a short little way to look through an entire array to see if it contains a certain value?


BBS Signature
blakemoso
blakemoso
  • Member since: Apr. 2, 2011
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 22:35:15 Reply

At 10/2/12 10:01 PM, blakemoso wrote:
On a side note, now that I've got my array with all the possibilities in it, whats the best way to check if one certain thing is present in the array?

It just occured to me that I could perhaps sort the array so that the value I want comes up first, then I can just compare the first item in the array to what I want... I think I'll do this unless there is another better way?


BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to Arrays with multiple possibilities 2012-10-02 23:18:17 Reply

At 10/2/12 10:31 PM, blakemoso wrote: Like, inside the filter for the location array, I need it to either return true if the options array contains one certain thing, and return false if it does not. I know I could check through each value in the options array individually to see if they match, but It seems a little long-handed... So would making another filter for the options array just to filter it down to check for the one item. Is there a short little way to look through an entire array to see if it contains a certain value?

Just use a for loop and check if each element matches what you're looking for, return true if any do, false otherwise