A Flash Developer Resource Site

Results 1 to 19 of 19

Thread: looking for something in an array

  1. #1
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    well, i'm still not very performant with arrays. i was just wondering if it's possible to look for something in an array. here's what i mean:

    i have an array:
    myarray = ["blue","red","black","yellow","green","orange"];

    i want a code to detect if "orange" and "yellow" are present in that array, no matter what their position is... is it possible?

    thanks.

  2. #2
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    I suppose a simple for loop would suffice. something along the lines of...


    for (n=0; n<myArray.length; n++)
    {
    if (myArray[n] == "blue")
    {
    // blue = true, or whatever you need to do with the info
    }
    }

    Could make this into a small function i suppose and pass variable of colour so you could check which colour you want, not just blue. And if you wanted to check for more, run the function again with new parameter.

  3. #3
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    oooh, that easy...?

    And if you wanted to check for more, run the function again with new parameter.
    in that case, i suppose i can also do

    for (n=0; n<myArray.length; n++)
    {
    if (myArray[n] == "blue" && myArray[n] == "orange")
    {
    //what i want to happen here
    }
    }

    well, thanks for the info, i'll try it as soon as i can ^^

  4. #4
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784
    Hey, Marmotte.

    I'm not the best at arrays either, but I don't think what you have in your last post will work. array[n] can't be both "blue" and "orange" at the same time, so the code will never run.

    I think what JonMack meant was a function like:
    Code:
    function display_color(choice){
    for (n=o;n<color_array.length;n++){
    if (color_array[n] == choice){
    //do something with choice variable
    }
    }
    }
    JonMack, I'm sorry if I've misunderstood what you meant, but I'm pretty sure the code as Marmotte wrote it will not function.

    BTW, Marmotte, are you still working on the fighting game? If so, I have something I'd like to discuss with you privately via email (japangreg@hotmail.com) I might be able to help you with some areas of your project. I would also like to get your opinion about a project I am working on that relates to your fighting game. If you are interested, or still looking for graphics, please contact me.

    hth
    japangreg

  5. #5
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    Yup, you can't do it that way Marmotte. What you're asking with:
    Code:
    if(array[n]=="blue" && array[n]=="orange"){
    
    }
    will never work. thats like saying:
    Code:
    if(varColor == "blue" && varColor == "orange"){
    
    }
    a variable can't be two things at once.

  6. #6
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    ok, let me explain why i asked that:

    i'm working very hard with my fight game right now (i have more than 80 characters available and some animated backgrounds, but i'm still concentrating with one and only character. i want the playability to be perfect before putting the other guys). so, my question about arrays was in fact a way to put up the combo movments. i already had a code for that, but well, as i'm not very performant with arrays right now... i wasn't able to use it perfectly.

    ok, for the combo thing, i was hoping something like:

    1)
    an array that keeps track of last downed keys (i already have it, it works fine. i also put a counter with it)
    so, imagine my array that checks the downed keys looks something like:

    myarray = ["left","right","right","up","down","MediumPunch","down","right","StrongKick"];
    (so it means i pressed all that keys during the last seconds)

    2)
    a code that checks the content of that array (the code i'm looking for), and allows special moves:

    example move: "down", "right", "StrongKick"
    ^ that combo correspond to the downed keys in the previous array, so the character executes his corresponding special move.

    of course, keys have to be pressed in the same order (for example, "right", "down", "StrongKick" will not work)

    you see what i mean? ^^

  7. #7
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784
    I think there's a more efficient (and cleaner) way to do this, but what about...
    Code:
    for(i=0;i<key_pressed_array.length;i++){
    if (key_pressed_array[ i]== "down"){
      if (key_pressed_array[ i+1]== "right"){
        if (key_pressed_array[ i+2]== "strong kick"){
            //execute super move
        }
      }
    }
    }
    I think there's a way of searching an array for a specific combination of elements, but I can recall it at the moment.

    hth
    japangreg

  8. #8
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    Ah, I do see. How about this. Have an offscreen MC that deals with special moves, coded like this:

    Code:
    onClipEvent(load){
      //DEFINE SPECIAL MOVES INDIVIDUALLY
      FireBall = new Array("down", "down_right", "right_punch");
      //DEFINE A MASTER SPECIAL MOVE ARRAY
      arrSpecials = new Array(Fireball, Dragonpunch, whatever);
    }
    
    onClipEvent(load){
      //get the player keyPress array handy
      arrKeys = _root.player.arrKeys;
      //loop through specials array
      for(specials=0; specials<arrSpecials.length; specials++){
        //loop through the array that holds keyPresses
        for(keyCheck=0;keyCheck < arrKeys.length; keyCheck++){
          //If the first key in the combo matches
          if(arrKeys[keyCheck] == arrSpecials[specials][1]){
            //continue checking (didn't want to write the code)
            //use a break; as soon as a key doesn't match the special.
          }
        }
      }
    }
    Kind of complicated? Yes, but it will work. If you want some more help, contact me on ICQ. I think that you should probably get your key press logging to include combination of directions and moves (ie, downright for down and right at the same time).

  9. #9
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    Japangreg's is the same as mine, but I just added the functionality for multiple special moves (using multi-dimensional arrays, oouuuu).

  10. #10
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    ! thanks for your help!

    let me take a deeper look at all your codes, i'll do the best as i can to understand them ^^

    devnull_2k
    about your combination suggestion (down+left at the same time) i remember having a problem with it. well, i'm not sure right now, but while peforming the famous hadoken combo (down, down+forward, forward, punch), the array didn't check the down+forward combination if i didn't release the "down" key before continuing... you see what i mean? haaa, my english...
    i'll try again to do that code for sure ^^

    japangreg
    about your first post here, please e-mail me and tell me everything ^^

  11. #11
    gskinner.com
    Join Date
    Feb 2002
    Location
    N.America
    Posts
    455
    A few quick notes for you marmotte:

    1) You WILL need a custom key handler to make the combos that you want work properly. It will have to test keydowns and return an appropriate composite key constant. A very simple example of what I mean:

    Code:
    myListener = new Object();
    Key.addListener(myListener);
    
    myListener.onKeyDown = myListener.onKeyUp = function() {
      // generate a direction keyCode
      var directionKeyCode = 0;
      directionKeyCode += Key.isDown(Key.LEFT) *1;
      directionKeyCode += Key.isDown(Key.UP)*2;
      directionKeyCode += Key.isDown(Key.RIGHT)*4;
      directionKeyCode += Key.isDown(Key.DOWN)*8;
      // now check if it has changed:
      if (directionKeyCode != oldDirectionKeyCode) {
        oldDirectionKeyCode = directionKeyCode;
        // trigger an action with this keyCode
        _root.newDirectionCommand(directionKeyCode);
      }
    
      // now, do the same type of thing with attack keys.
    }
    This will allow you to control this like an arcade console with an 8 way joystick and the ability to press multiple attack buttons at once (although this is limited by the keyboard in some cases). It also breaks actions into integers, which are more efficient to work with in pattern matching. It will take more refining, but hopefully it helps.


    2) devnull's suggestion is a good start, but is backwards. You should be checking from the end of the saved moves array backwards, immediately discarding any patterns that don't match. Otherwise you are wasting HUGE amounts of resources trying to pattern match every combo against every iteration of the key array. For instance, a set of 5 combos (@3 moves each) matched against an array of the last 8 keys would take exactly 120 comparisons every time you checked. Doing it in reverse would take a maximum of 15 comparisons, and a minimum of 5 to check.

    Hope it helps.

    Cheers,
    ZE.

  12. #12
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    i just tried japangreg's code (it's the one i understand the most at the moment ^^). it works fine (i changed it a bit of course).

    the problem i was posting earlier, i just encountered it again; now i can explain better:

    you already knows it, i have a simple array that keep in memory the last 6 keys pressed (with a timer). when i press "down", the indication "down" appears in that array, naturally. but if i press the "left" key whitout releasing "down", nothing more appears. i have to release the "down" key before in order to make the "left" key to appear in the array...

    ok zeroEra
    i'll take a serious look at your code, it seems you actually resolves that "problem". thank you very much for your help anyway...

    oh, is there a way to erase all data that are in an array at the same time?

  13. #13
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    Interesting zeroERA. I didn't think about that. Thank you.

  14. #14
    Didn't do it. japangreg's Avatar
    Join Date
    Mar 2001
    Location
    \o/ |o| |o_ /o\
    Posts
    784
    Hey, Marmotte. I just sent you an email.

    I think the easiest way to reset an array is to just myArray=[];. Any one else know a better way?

    hth
    japangreg

  15. #15
    gskinner.com
    Join Date
    Feb 2002
    Location
    N.America
    Posts
    455
    Right on japanreg.

    myArray=[];

    Cheers,
    ZE.

  16. #16
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    just a thought but wouldn't it be faster if you used a string rather than an array? you could use bitwise operators to store and check up to eight key combinations/byte. it would go really fast.

  17. #17
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    mmmh, BlinkOK, can you give a small sample, please?

    i mean, maybe you can explain a little bit more what you just said...?
    or give a sample code?
    for now, probably because of my english, i don't get what you are thinking about...

  18. #18
    I'm feeling supersonic kdsh7's Avatar
    Join Date
    Jul 2002
    Posts
    356
    how feasible would it be to use this for cheat codes? like you type in 'life' to increase your life? same principle i guess, but would it demand much cpu to detect and match moves to the array?

  19. #19
    gskinner.com
    Join Date
    Feb 2002
    Location
    N.America
    Posts
    455
    BlinkOK,

    Feel free to correct, but I believe the array of integers would be just as fast, or faster. After all, a string is simply an array of characters (8 bit integers) wrapped in a convenient object. One would assume the array would be faster as it would not have the overhead of the String wrapper, and you would not need to split or substr out required characters each time you test (unless you did it once, in which case you would simply wind up with an array again - which would then have to be converted to binary integers in the background for bitwise operations to work --> back where we started).

    I suppose there's a chance it would be faster if bitwise operators have been optimized in MX. You could certainly use bitwise operators to optimize the code I posted slightly (using bitwise shift instead of multiplying by 2,4,8), but the resulting speed gain would be insignificant.

    Even if there is a small optimization to be gained, I doubt the additional complexity to the code would be worth it (unless you are already very familiar with using bitwise operators).

    If you'd like some info on bitwise operators, here is an old article that is a good introduction:
    http://builder.cnet.com/webbuilding/...ripter/082698/

    Just my 2c,
    ZE.

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