A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Grand-old Collision detection

  1. #1
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,992

    Quick way to find something in an Array (Better title)

    Say I've got about 30 enemies on screen which are all after one hero character.

    What is the quickest way to do a hit test to make sure the enemies aren't touching one another?

    I have a method which works pretty good, but I'm just checking if anyone might know a faster way.

    I'm currently using something like this:

    - Enemies is an objects
    - Enemies are ID's by the index of several array statements in enemies
    - Enemies has an array called SPOT which is the tile location of the enemy (5_4, 20_40, 100_300)
    - After the enemy has moved, it does a check like this:

    (NOTE: I'm doing this from memory, so forgive any code errors)
    Code:
    //First we get the current enemies' spot
    mySpot = enemies.SPOT[index]
    //Then we replace the current enemies' spot with nonsense
    enemies.SPOT[index] = "NASHI"
    //Next we convert the enemies.SPOT array into a big arse string
    
    eSpot = enemies.SPOT.toString()
    //Then we check to see if there is any instance of mySpot in the 
    //big arse string...
    
    if (eSpot.indexOf([mySpot]) == -1){
    break;
    } else {
    //do the old fashion for loop thing to find out exactly where the spot is...
    }
    Something like that.

    Does anyone know of another method?

    EDIT: Heh, heh. Sometimes, it just helps to type stuff up. I found a way to speed this up.

    (I think...)

    -pXw
    Last edited by WilloughbyJackson; 10-17-2003 at 04:21 PM.

    Play - Live - Work - Enjoy

  2. #2
    Moderator
    FK Junkie
    TiefighT's Avatar
    Join Date
    Aug 2000
    Posts
    683
    You are doing that for each enemy object, each frame, correct?

    If that is the case, then the most straight-forward way to speed it up would be to centralize it. Have a single method that fires once every frame (or better yet, only in frames where either an enemy or the hero moves), and then goes through and checks each enemie's position against the hero's.

    I played around with something similar a while back, you can check out this thread for some further explanation and downloadable exampels.

    Good luck

  3. #3
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,992
    Originally posted by TiefighT
    You are doing that for each enemy object, each frame, correct?

    If that is the case, then the most straight-forward way to speed it up would be to centralize it. Have a single method that fires once every frame (or better yet, only in frames where either an enemy or the hero moves), and then goes through and checks each enemie's position against the hero's.
    All the code is centralized (first frame of the movie) and I'm actually only doing it when the enemies moves to a new spot.

    I kind of mistitled my post, it should have been about getting information from an array. I'm actually more interested in a fastest way to search an array statement in Flash.

    Some languages have a very nice feature where you can search for a specific value in an array and return and return that value's index. As far as I can tell Flash MX does not have such a feature.

    I played around with something similar a while back, you can check out this thread for some further explanation and downloadable exampels.
    I'll check it out. Thanks..

    -pXw

    Play - Live - Work - Enjoy

  4. #4
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    i would do it tilebased. so the dude checks where he's gonna go BEFORE he moves and then if the tile is free he moves to it. if it's not he check to the right or left until he finds a free tile. if there is not free tiles he doesn't move.
    you don't need to have tilebased graphics for it just tilebased hittesting.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  5. #5
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182

    :)

    Hey,

    You just inspired me to start working on something

    Ok here;s what I got:

    Make an enemyMC with 3 frames, on frame 1 the normal movement frame, on frame 2 the hitTest frame, and on frame 3 a BLANK keyframe with a gotoAndStop(1); action...

    Now draw the enemyMC on stage and name it 'dupe0' and put this code on it:
    code:

    onClipEvent (load) {
    var timer = 0;
    var dir = random(4);
    var count = 20;
    if (_name == "dupe0") {
    for (i=1; i<count+1; i++) {
    this.duplicateMovieClip("dupe"+i, i+10);
    }
    }
    }
    onClipEvent (enterFrame) {
    if (timer == 0) {
    dir = random(4);
    timer = 10;
    } else {
    timer--;
    }
    if (dir == 0) {
    _x += 4;
    } else if (dir == 1) {
    _x -= 4;
    } else if (dir == 2) {
    _y += 4;
    } else if (dir == 3) {
    _y -= 4;
    }
    if (_x>550) {
    _x = 550;
    dir = random(4);
    } else if (_x<0) {
    _x = 0;
    dir = random(4);
    } else if (_y>400) {
    _y = 400;
    dir = random(4);
    } else if (_y<0) {
    _y = 0;
    dir = random(4);
    }
    }


    It's a very simple random movement... I think you're able to understand this, and if not just ask...

    Now on frame 1 of the _root put this code:
    code:

    this.onEnterFrame = function() {
    for (var i in _root) {
    if (this[i].hitTest(_xmouse, _ymouse, true)) {
    this[i].gotoAndPlay(2);
    }
    }
    };


    What this does is check all the object on _root, and see if they are hitting the current mouse postion, now I think that the movement of the enemy's are slowing the player down more then hte hitTest itself

    PS;
    There is a check to see if the this[i] object is a MC (so hitTets wont be pulled on buttons) but I couldn't remember it now...

  6. #6
    Senior Member Mad-Sci's Avatar
    Join Date
    Mar 2000
    Posts
    2,756
    http://mad-sci.lazoom.com/
    check out the Array hitTest() fla the last button on the left.

    ms

  7. #7
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,992
    Originally posted by BlinkOk
    i would do it tilebased. so the dude checks where he's gonna go BEFORE he moves and then if the tile is free he moves to it. if it's not he check to the right or left until he finds a free tile. if there is not free tiles he doesn't move.
    you don't need to have tilebased graphics for it just tilebased hittesting.
    I'm actually doing tilebased collision already. (hitTest() for a bunch of enemies constantly running around would be killer for this game...)

    The problem is due to the nature of the game, the enemies are constantly moving around both on-stage AND off-stage.

    My current stage size is...

    1000 px x 800 px (Full scroll size)

    Since I have to keep track of them off stage, how would I do the check without attaching anymore tiles?

    (Once again, my current method works for what the game is, I'm just seeing if anyone has anything better).

    SaphuA:

    I was dancing around this but I'll just admit it. The enemies must check to see if they hit each other besides checking the hero because something happens when the enemies collide. Something... bad...

    but it can be used for the hero's advantage (and NEEDS to be used to the hero's advantage!).

    I've got some new ideas just from talking to everyone here.

    Mucho-mucho love

    Any if anyone else has any other suggestions, just throw them at me.

    -pXw

    Play - Live - Work - Enjoy

  8. #8
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,992


    Coming Halloween.. (except it will look a hell of a lot better.. )

    EDIT: Of course I was standing still when the snapshot was taken..

    -pXw
    Last edited by WilloughbyJackson; 10-17-2003 at 09:07 PM.

  9. #9
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    right now they will "brush" each other now and again but ALMOST never hit. if you reduce the tile test to just the center of the mc they will brush quite a bit and collide every so often;
    This shows the logic;
    http://www.nlc.net.au/~oceana/games/...avoidance.html
    This shows 30 mc's at work;
    http://www.nlc.net.au/~oceana/games/...oidance30.html
    Here are the .flas;
    http://www.nlc.net.au/~oceana/games/...navoidance.zip
    For very little extra effort you can add a hittest when the npc finds another npc in it's next tile by getting the mc's instance name out of the tile array.
    blink
    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