Source Code: Shape-based Hit Detection in 8

A while back I posted a demo of shape-based hit detection in Flash 8. At the time, I wasn’t allowed to post the source code, but promised I would later. So here it is.

The com.gskinner.sprites.CollisionDetection class is really simple to work with, there is a single static method called checkForCollision with four parameters:

checkForCollision(movieClip1,movieClip2,alphaTolerance);

movieClip1, movieClip2 – The MovieClip instances to check for collision.
alphaTolerance – a number from 0 to 255 that specifies the transparency tolerance when testing the collision. A higher number will increase the tolerance (ie. allow more transparent parts of the MovieClip to register collisions). Defaults to 255.

Returns a Rectangle object specifying the intersection of the two MovieClips in the _root coordinate space, or null if they do not intersect.

Check out the original post for more information on how the class works.

Click here to download. It includes two simple demos to help you get started with it.


Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

97 Comments

  1. Hi grant – looks good.

    A couple of questions:

    I gather this is derived from the classic bitmask/ XOR method – is that right? Also, how well does it scale? Have you tried it with larger numbers of objects?

  2. Alias,

    Similar idea to XORing, but taking advantage of the player functions – it’s basically drawing clips onto a bitmap in a specific color using a difference blen and looking for a specific color in the result.

    It runs better than I would have expected, partly because I’m using boundary intersections to minimize the bitmap dimensions, and rule out clips quickly.

    I have a version that’s fully integrated with my proximity management logic that runs really well, because it only uses a single BitmapData object. I might release that one in the future.

  3. Hi Grant,

    Cool – I’m working on a similar proximity manager which uses spatial hashing tables. I’ve been taking a more geometric approach to narrow phase collision detections so far, but I think this approach is definitely worth considering as well.

    Any thoughts on collision resolution with this approach? Have you thought about maybe using the graphical intersection data to determine a penetration vector?

  4. Yes, I’ve been thinking about doing that. Given the velocities and the intersection boundaries it should be fairly simple to get a rough vector.

  5. lol why does everything have to have an ugly drop shadow or blurs in everyones experiments now…. it’s like when lens flare was introduced in photoshop

  6. Offtopic: As i don`t see answers on other pages, i`m writing to the latest post. How much is gProject? 35$ or 41.50 ? kagi.com tries to include VAT and asks for shipping addres, but i dont need anything to be shipped, just the download for 35$! why do they want to charge me additional 6 bucks? ok, its not that much, but still kinda silly..

  7. Hey, I’m using this code in a game I’m making.

    I’ve got this code in the car Movie Clip:

    import com.gskinner.sprites.CollisionDetection;

    onEnterFrame = function () {

    for (n=1; nthisName) {

    if (thisName == _root.activeCarNo) {

    _root.speed *= -1;

    _root.cars[carNo].speed *= -1;

    } else {

    _root.cars[carNo].speed *= -1;

    _root.cars[thisName].speed *= -1;

    }

    }

    }

    }

    };

    it doesn’t work for some reason???

    I also used the same code in your example (with the spinning thing) to make each ball hit each other, which worked???

    What’s wrong?

  8. the ‘for’ statement should be:

    for (n=1; n

  9. ? it doesn’t work

  10. when i try to refer to the mc within another movie clip the hit detection doesn’t seem to work

    if (CollisionDetection.checkForCollision(this,target,120,_root.player)) {

    play();

    _root.player.health -= 2

    delete(onEnterFrame);

    }

    and i set the target to body(a mc in the mc of player) no hits end up being detected.

    any ideas?

  11. sorry for my english!

    it’s very advanced for me, y just need a variable that tells me the _x and _y position of the collision. i’m buileding a city map, that when the people put the two streets they find, the map moves to de collision of two,

    i hope u understand

    thanks a lot

    Diego

  12. This is awesome, but I can’t seem to open the .fla file with Flash MX 2004 (PC). Do you have a version that works for that?

    Erik

  13. Hey paul

    i’m kind of a newb and only 12 but i know how to

    fix yur problem:

    “checkForCollision(this,target,120,_root.player)”

    The “target” should b a target path to the movieClip its checking for collision with

    ur welcom

  14. Hi,

    this looks very interesting.

    One question though: I tried the example but nested the rotating shape inside another mc. When this parent mc is not positioned on (0,0) the hitdetection does not work properly anymore.

    Any thoughts on how to sove this problem?

    Thanks,

    Arjen

  15. I am a very big newb. and am hoping that you guys can help me out because you seem to know what your talking about…

    Once i have the source script from a site… how do i put it to use…..

    i have flash mx proffesional 2004 v7.2

    please help….

  16. Is there any problems with masks ?

    This code works perfectly on my clips, but if un use a setMask on it, this collision checker returns nothing… :-/

    Excellent work anyway !

  17. Excellent work gskinner.I must congrat you for this work.In the past I tried checking collisions using bitmaps but it was too hard as It didn’t “hit” me to build a lcass for this (probably because my oop knowledge then was limted)

    As I played with CollisionDetection class I saw a small(that could become big when you are developing something that has big movieclips) flaw.Working with bitmaps as we all know is very tricky and if you don’t want that your little swf game take up the entire Comp Memory you should be sure that the memory is freed up as the bitmap is not needed anymore.

    In my opinion you should add right before it helped me a lot with a big bitmap

    My Regards,

    Oliver

  18. Great!

    I’m trying use this in my flex project. It works great, but…there are times that my plyer crashes. And i fihures out that it’s the blendmode that is given me problems…. Any ideas??

  19. I have to install de class file? o just writing this setence “import com.gskinner.sprites.CollisionDetection”

    the script works?

  20. hi what if I want a collision detection with an object with the alpha property set to zero?

  21. hi guys

    what I meant in the above post is what if you want to check the collision of an object inside an object for example:

    CollisionDetection.checkForCollision(_root.tubo,this.sombra);

    this aint working and I guess is because we’re not really passing a MovieClip (checkForCollision(p_clip1:MovieClip,p_clip2:MovieClip…) to the function but an adress of the movieclip

    tnx in advance

  22. Hi Grant!

    This class was very useful but there is still a bug.

    in the static checkForColllision function you have:

    var bounds:Object = {};

    bounds.xMin = Math.max(bounds1.xMin,bounds2.xMin);

    bounds.xMax = Math.min(bounds1.xMax,bounds2.xMax);

    bounds.yMin = Math.max(bounds1.yMin,bounds2.yMin);

    bounds.yMax = Math.min(bounds1.yMax,bounds2.yMax);

    but you have switched the Math.max and Math.min methods. change the Math.max to Math.min and vice versa and it works like a charm =) !

    Sincerely,

    Reinier Rossen @ http://www.mediamonksgames.com

  23. I am having the same problem as several others:

    The function does not seem to work when using MovieClips outside of the root. For instance:

    checkForCollision(mario_mc.foot_mc, goomba_mc) would not work because I’m checking for “foot_mc”, which is inside of a movie clip.

    Is there any workaround to this?

  24. I’ve just tested with clips in different parents, and it seemed to work fine. I’m not certain if that’s because the version I have locally is more up to date than the one that was online, or if it’s simply because I’m not reproducing the issue properly.

    I’ve updated the zip file with the latest version of the class, and added a third demo FLA that shows it working.

    If you can reproduce the issue with this file, please let me know – I’d like to see examples of the problem.

  25. Ok. I tracked down an issue with nested clips thanks to an example provided by Eric. I’ve updated the zip file with the latest class file. It should now work properly with nested clips, and any transformations (scaling, rotating, etc) of the targets or their parents.

    I also removed the fourth parameter (scope), as it will not work correctly with this updated version. The intersection rectangle will always be returned in the _root’s coordinate space.

  26. Great class. However, I’m having some trouble utilizing this within my game. I have the collision detection placed on my character’s MC and I’m using it within the motion coding. Basically, it checks if the key is down and the character isn’t touching a wall, allow the character to move.

    The problem with this is that if the character IS touching a wall, it won’t allow movement to get away from it, so the character is stuck.

    Previously I could work around this when using hittesting by placing a “+speed” variable after the x/y coordinate detection, but with this class, I’m not sure if you can do that. Could anybody help?

    This is the code on the character (part of it, at least)

    onClipEvent (enterFrame) {

    speed = 7;

    if (playable==true) {

    if (Key.isDown(Key.LEFT) && !(CollisionDetection.checkForCollision(this, _root.boundary, 10))) {

    this._x -= speed;

    this._xscale = -100

    }

    if (Key.isDown(Key.RIGHT) && !(CollisionDetection.checkForCollision(this, _root.boundary, 10))) {

    this._x += speed;

    this._xscale = 100

    other than that, the code works great. The hit detection is much more accurate that old methods.

  27. I really need some help with a game a friend and I are making. I need a script that can detect a wall that is very obscure, kind of like your shape above but the player can’t get behind the wall. I have a wall script I’m using now but it requires that I put tiny shapes around the object. I am trying to overcome the bounding box that is created from contering the object to a symbol. I want to use the arrow keys and for some reason I can’t use yout script found here. I’m kind of bad with action script but I know some basics. If someone could post a solution or send me an Email at LJW10000@myactv.net I would be greatly appreciative.

  28. I have testet the new sourcecode which should work with nested movieclips, but I cant make it work? Ive just opened the “CDDemo1.fla” file and made a new movieclip inside both the mc1 and mc2 movieclip and called them hit1 and hit2. As I understand it should it work if I write as this:

    var collisionRect:Rectangle = CollisionDetection.checkForCollision(mc1.hit1,mc2.hit2,120);

    This dont work. Can anyone tell me why?

    Thanks

  29. I see how the example can display a rectangle of the collision area, but how did you get it precisely in your first example? The circle in the upper-left corner shows the EXACT area of collision as opposed to a rectangle covering all area.

    Can you release that code?

    Thanks!

    Dan

  30. Hello Grant,

    This collision detection class is great, but there is one problem. During run time, when you make the size of the window, which holds the swf, bigger, the collision detection doesn’t work anymore.

    This problem also occurs on the 2 demo’s that are included in the zip.

    Is there any solution to that problem?

    I have also created a game using you collision detection class. If I resize the window my game is running in the collision detection doesn’t work anymore.

    Kar Ching

  31. Hi Grant,

    You saved my day! I was all day frackin around with hittest and not getting it working cause I need to have a collision detection with a line drawn by the drawing api and flash puts a hitbox around it (grrrr)… So, there I was, headache and all, till I found this website.. Thanks!

    greetings,

    Jesse//

  32. Grant,

    Any possibility of converting this to AS3? If so, I would love it as soon as possible! Thanks,

    Eric

  33. Nevermind, I converted it.

  34. Hi Grant and readers,

    We have an actionscript 3 version of the skinner collision detection class. You can download it on our blog:

    http://labs.boulevart.be/index.php/2007/06/08/skinner-collision-detection-in-as3/

    It’s not yet been tested to it’s limits so improvements are always welcome.

    Thanks Grant for the amazing stuff you do and for the source code you share with us.

    It’s been a big help for us.

  35. hi, this looks really cool but i’m a newbie to flash. basically i have 4 floating movie clips that can be clicked on, they then motion tween themselves to the center of the stage and back again. at the centre of the stage i have a persons head. and i want it to flash red when the floating movie clips fly into it. i know no action scripting but any way you could help me out here?

  36. Hi, is there a simpler version of this script which is flash 7 or even 6 compatible? the current script returns a area of intersection which i dont need, a simple return true/false would be great. Any help is much appreciated : )

  37. Any chance we could get a peek at the version that uses a single BitmapData? I’m assuming you allocate some maximum size BitmapData then do all of your operations to different sections of it? What kind of performance difference do you realize?

    Also, any tips on what the biggest offenders are in the algorithm? Should I look to minimize the amount of overlap in the objects (make the BitmapData’s smaller), or is just the allocation and drawing of the BitmapData’s that dominates the equation?

    In my case, all of my objects are BitmapData objects, but they may be rotated or scaled… is this still going to be the best option for pixel perfect detection (I don’t have any shapes or vectors)?

    Thanks,

    Troy.

  38. Hey Kar Ching,

    I stumbled on your resizing problem a few months ago when I had to build a projector version of a game using Flash.

    I did some research on the internet about flash bug and resize and found nothing on how to get it working with normal resize.

    By tracing Gran’s code I observed that it’s happening because after resize flash draws the bitmapData in an eroneus location.

    I will do more research…until then if you want to bypass the issue just add

    Stage.align=”TL”;

    in your fla.This will fix the collision detection

    Until next time…

    Regards,

    Oliver

  39. this was exactly what I need! I love u man. haha =D

  40. I want to thank first for this great example of collision detection and say some words about it. I found some mistakes and here there are:

    1. The first boundary check it could be done by using the default hitTest() method = clip1.hitTest(clip2) but that’s optional

    2. You must change the xMin/xMax and yMin/yMax assignments. It calculates different coordinates and don’t return the real image boundary.

    3. The thing I said in 2. is actually not necessary because you these xMin/xMax and yMin/yMax methods to give the “img” object _width and _height parameters. We’ve got our big error here. Done in this way works perfectly but if the Stage is not zoomed! I mean if it is in its original size, when you make it fullscreen for example it doesn’t work. I’ll explain why after a few lines.

    Here’s my way: you give the “img” object these _width and _height parameters:

    1. You create a movie clip in your flash project with these parameters: _x=0; _y=0; _width=Stage._width; _height=Stage._height (the last two parameters is the original size of your flash project). This movie clip appears as a stage mask or something like that, that’s why I’m giving it this name “stage_mask”

    2. Make the “stage_mask” with 0% _alpha, so it can’t be visible;

    3. Use the path to your “stage_mask” Movie clip in your CollisionDetection.as (e.a. _root.gameclip.stage_mask)

    4. Add this to the “img” initiate in the CollisionDetection.as file:

    _root.gameclip.stage_mask._width, _root.gameclip.stage_mask._height

    instead of the bounds2.xMax-…. I don’t remember it 🙂

    What actually make it don’t work in the previous version:

    when in 200% zoom for example the .getBounds method returns the xMin/xMax and yMin/yMax coordinates of the movie clip on the stage (calculated with these 200% of zoom) which means if your MC’s width is 100 pixels it returns 200 because of the 200% zoom, but when you make flash draw a 200 pixels rectangle on the stage and you’re in 200% it actually appears as 400 pixels because of the zoom! What it actually happens is that the image you want to draw has less size than its movie clips. And so the img.getColorBoundsRect(…) method can’t find anything.

    If someone has some questions about this or want the reworked CollisionDetection can write me an e-mail: ivo_yankulovski@abv.bg

  41. I tried using this to make a game where you avoid astriods in a space ship and i attched this script to the layer, and its not working. Can someone tell me why

    if (CollisionDetection.checkForCollision(_root.spaceship_mc,_root.astriods_mc,255)) {

    gotoAndPlay(“you lose”, 1);

    } else {

    this.gotoAndStop(1);

    };

    //end of code

    it keeps making the game restart everytime I try to go to that scene…Please help.

    Thanks

  42. I would also like to be able to make my project fullscreen without the collision detection stopping working! I understand Stage.align=”TL”; will fix it. But my project does not suit being aligned to anything but the center.

    Any help would be fantastic.

    Thanks,

    Jon

  43. There is a version in as3?

  44. I made a test program to check collisions of lots of objects on my blog, and have made a tweaked version of this with a few bits borrowed from troy gilberts version and i gained a decent speed up in some cases. I hope it’s ok to distribute my modified version, i’ve given you full credits and link in my blog. I’m going to try merging this with your proximity grid system and see if that improves the speed by a decent amount. Let me know if it’s ok to share my modified version on my blog, it’s my first actionscript 3 demo – only been programming in AS3 for a short while 🙂

  45. Sorry, bit of a noob at the whole programming lark – let me see if I’ve got this right. THe last parameter (alpha tolerance), will mean that the bigger the number, the more faded out the MC can be for the Collision Detection to detect something. Is that right – or the inverse of that?

  46. Hi –

    I just wanted to extend my thanks for your foundational work on collision detection. I just included Troy Gilbert’s most recent version of your class in an open-source piece. A link to the project can be found below:

    https://netfiles.umn.edu/users/frah0005/www/dynamicSpace/dynamicSpace.html

  47. Hi all , I’m using checkForCollision for two MovieClips, One mc from my library and the other from dinamyc lineTo movie clip.

    I’got this error:

    1046: Type was not found or was not a compile-time constant

  48. Ozgur Uksal May 5, 2008 at 8:38am

    Hi, Grant. This is a great work.

  49. Hi,

    I used this class and it was really great, but i found an error when stage resize.

    Because the coordinates change and when the script tries to getBound() the values are wrong.

    So i update my class to include this code:

    // get bounds:

    var bounds1:Object = p_clip1.getBounds(_root);

    var bounds2:Object = p_clip2.getBounds(_root);

    var nDifW:Number = Math.round((Stage.width – 800)/2);

    var nDifH:Number = Math.round((Stage.height – 600)/2);

    bounds1.xMin = bounds1.xMin + nDifW;

    bounds1.xMax = bounds1.xMax + nDifW;

    bounds1.yMin = bounds1.yMin + nDifH;

    bounds1.yMax = bounds1.yMax + nDifH;

    bounds2.xMin = bounds2.xMin + nDifW;

    bounds2.xMax = bounds2.xMax + nDifW;

    bounds2.yMin = bounds2.yMin + nDifH;

    bounds2.yMax = bounds2.yMax + nDifH;

    I hope that can help someone, since i lost one day until i discover what is going wrong 🙂

  50. Hi,

    Nice class, thankyou. I can get it working fine in player 9 but when i test publish for player 8 it gets buggy – seems to be translating incorrectly. I see that you recommend 8r50 but all the adobe documentation for the various methods used says they’re good for player 8.

    Can you point out the 8r50 code or suggest a player 8 workaround please?

    (I noticed this because my hit object is heart-shaped instead of square or round).

    Thanks

  51. Further to my player comment. I realised it was actually a drop-shadow filter on the parent clip that was causing the problems. I’ve re-jigged my clips so as to avoid this and it works fine in player 8 now – albeit with the Filipe Murteira stage-resize friendly code. Brilliant – thanks guys!

  52. Hi Filipe Murteira ,

    I have the same problem, thank you very much for share fix class. It’s very usefull

    Nike regards

    Aries (Vietnamese)

  53. Great class btw. I’m finding that by using setMask the collision doesn’t work. Has anyone found a solution to this? I’ve tried exploring around with getBounds to no avail.

    Thanks!

    brian

  54. I was banging my head against a wall trying to get the latest version of this routine to work correctly when our module was within a container scaling it to fullscreen (not the built-in Flash function, but using swhx to achieve the job). Here’s what we found…

    When you use mc.transform.concatenatedMatrix it rattles all the way back to the absolute _root. However, if any movieclips are set with _lockRoot=true on the way, when you getBounds(_root) you’re working with the co-ordinate space of the _lockRooted movieclip, which could (and, in my case, was) scaled differently. Taking the concatenatedMatrix as my cue, I reworked the routine like this:

    // draw in the first image:

    var mat:Matrix = p_clip1.transform.matrix;

    var clipParent:MovieClip = p_clip1._parent;

    while (clipParent != _root)

    {

    mat.concat(clipParent.transform.matrix);

    clipParent = clipParent._parent;

    }

    This manual concatenation has fixed all my fullscreen issues. I’ve also made two other changes, one vital to prevent memory leaks, and one just to beautify it a little (IMHO)…

    At present, the bitmap used for the collision detection is never disposed of. This is A Bad Thing. I’ve added img.dispose() immediately after the getColorBounds() command.

    And we have in our company code library a function to return the bounds of a clip as a flash.geom.Rectangle object. Very simple function, it just remaps the xMin, yMin etc. to the correct properties for a Rectangle. By doing this at the top of the code, where the bounds of the clips are calculated, you can tidy up the code a hell of a lot:

    // rule out anything that we know can’t collide:

    if (!bounds1.intersects(bounds2))

    {

    return null;

    }

    // determine test area boundaries:

    var bounds:Rectangle = bounds1.intersection(bounds2);

    Much nicer.

  55. what a bad tut.. atleast put an example. I can’t figuire it out

  56. zzzz – not to belabour the obvious, but this is not a “tut”, and it does include an example file. There’s not much to “figure out” other than copying and pasting the code.

  57. o my bad i just saw it now

  58. Hi Grant

    I had a fun day today working with your Collision Detection script. Its very slick. I have adapted it for a maze based game but I was hoping to try and add a way of restricting the moving object to the edge of the maze walls instead of walking through them. I would paste my effort here but its a little embarassing 🙂

    I wonder… how would you do it?

    andy

  59. Sebastian Salzgeber September 23, 2008 at 11:35am

    I didnt get it working with the fixes from the comment. Can someone from above please link his class where the fullscren/resize-Bug is fixed?

  60. Possible check CollisionDetection of mouse pointer on HitArea Movieclip?

  61. i’ve tried with

    }

    var currPoint:Point = new Point();

    var mouseListener:Object = new Object();

    mouseListener.onMouseMove = function() {

    currPoint.x = _xmouse;

    currPoint.y = _ymouse;

    checkCollision();

    };

    function checkCollision() {

    var collisionRect:Rectangle = CollisionDetection.checkForCollision(item3, currPoint, 120);

    trace((collisionRect == null) ? 1 : 2);

    }

    It rertune error

    **Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 9: Type mismatch.

    var collisionRect:Rectangle = CollisionDetection.checkForCollision(item3, currPoint, 120);

  62. What a lifesaver. 😀

    Thanks for sharing this! I can’t believe that Flash was so lacking in this department… I hadn’t even considered the bounding box being a problem until I tried working with it.

  63. I ´ve tried a lot, spending many time changing the class so to make it work with fullscreen, but it was impossible, even using the post thas have been made here. Could anyone post the final class with these bugs fixed?

    Thank you.

  64. Hello!

    This class is exactly what I needed for a game I’m working on. However, I too have had no luck with the proposed fixes to full-screen issue.

    I have tried both Filipe Murteira’s fix and the Stage.align=”TL”; trick, both singly and in combination, and neither have worked. In fact, Filipe Murteira’s code also seems to be causing it to stop working even without resizing the screen.

    Also, Chrisf: Although it’s been a year since your post and you’ve probably either fixed you issue or stopped looking, you may wish to try if (CollisionDetection.checkForCollision(_root.spaceship_mc,_root.astriods_mc,255)!=null). I use something similar in my game, and that part seems to be working.

  65. Having been prodded for the full source of my modified version of this function, here it is. This works for us and has been used in a whole bunch of programs; it is, however, still AS2 (we’re moving over to AS3 quite gradually due to the wealth of library functions we’ve amassed over the years).

    static public function checkForCollision(p_clip1:MovieClip, p_clip2:MovieClip, p_alphaTolerance:Number):Rectangle

    {

    // set up default params:

    if (p_alphaTolerance == undefined)

    p_alphaTolerance = 255;

    var bounds1:Rectangle = ClipUtils.getClipRect(p_clip1, _root);

    var bounds2:Rectangle = ClipUtils.getClipRect(p_clip2, _root);

    // rule out anything that we know can’t collide:

    if (!bounds1.intersects(bounds2))

    {

    return null;

    }

    // determine test area boundaries:

    var bounds:Rectangle = bounds1.intersection(bounds2);

    // set up the image to use:

    var img:BitmapData = new BitmapData(Math.ceil(bounds.width), Math.ceil(bounds.height), false);

    // draw in the first image:

    var mat:Matrix = p_clip1.transform.matrix;

    var clipParent:MovieClip = p_clip1._parent;

    while (clipParent != _root)

    {

    mat.concat(clipParent.transform.matrix);

    clipParent = clipParent._parent;

    }

    mat.tx -= bounds.left;

    mat.ty -= bounds.top;

    img.draw(p_clip1, mat, new ColorTransform(1, 1, 1, 1, 255, -255, -255, p_alphaTolerance));

    // overlay the second image:

    mat = p_clip2.transform.matrix;

    clipParent = p_clip2._parent;

    while (clipParent != _root)

    {

    mat.concat(clipParent.transform.matrix);

    clipParent = clipParent._parent;

    }

    mat.tx -= bounds.left;

    mat.ty -= bounds.top;

    img.draw(p_clip2, mat, new ColorTransform(1, 1, 1, 1, 255, 255, 255, p_alphaTolerance), “difference”);

    var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);

    img.dispose();

    // if there is no intersection, return null:

    if (intersection.width == 0)

    {

    return null;

    }

    // adjust the intersection to account for the bounds:

    intersection.x += bounds.left;

    intersection.y += bounds.top;

    return intersection;

    }

    public static function getClipRect(clip:MovieClip,reference:MovieClip):Rectangle

    {

    if (reference==undefined)

    reference = clip._parent;

    var b:Object = clip.getBounds(reference);

    return new Rectangle(b.xMin,b.yMin,b.xMax-b.xMin,b.yMax-b.yMin);

    }

  66. Forgot to remove the reference to ClipUtils class when calculating bounds1 and bounds2. Delete the “ClipUtils.” bit and it should all work fine.

  67. Ah! Awesome job Steve! That script solves the resizing the stage error like heaven

  68. It looks wonderful. I really want to go through it

    I was going through a personal gaming project.

  69. How can we do it in flash 5?

  70. A significant share. For sharing to thank me something that i’m looking for. Work up!

  71. it starts laggin for me..??.. can anyone explain!?

  72. It’s a great class. It could used it for a project but it works only local. It doesn’t work when I run it on the web. I placed my .swf file and the com folder in the same folder at the server. What I’m doing wrong?

  73. @Steve Anderson: “At present, the bitmap used for the collision detection is never disposed of. This is A Bad Thing. I’ve added img.dispose() immediately after the getColorBounds() command.”

    since the img object is a var defined in this function and not referenced outside function scope, wouldn’t the variable expire and be removed from memory by garbage collection automatically when the function completes? Do you really have to .dispose() manually?

  74. THANKS A TON STEVE!!!

  75. I got problem, it doesn’t work properly on browser, any idea?

  76. Man, tanks a lot! You save the day! God bless u!

    Take care,

    Cleverson – Brazil

  77. Thanks so much Grant!!

    been looking for ages for a better way to do hitTest

    but none of em that I have found can match yours!

    Cheers

    Q-UK

  78. Great work, thanks for sharing

  79. perfect, i just started making a game and this is much nicer than the regular old hitTest. collision detection with enemies feels a heck of a lot better now!

  80. Hi!

    I am having a problem with this code. I am using

    if(CollisionDetection.checkForCollision(_parent._parent.poze.bloc2st_mc,_parent.sageti.sageata.arrow_1_mc,120)!=null)

    {

    trace(‘bloc2st’);

    }

    but it doesn’t work. If I write just ‘_parent._parent.poze’ it works. ‘bloc2st_mc’is a movieclip inside an external swf that I load in ‘poze’ on the stage.

    Any ideas why it doesn’t want to recognize bloc2st_mc?

    Thank you!

  81. wow, this looks extremely confusing to me…. anyone here could say how i shall use it? and not what it does??? just a simple effect like…. a mouse controled object hits a “square” then they both will be unloaded…. just a ex. that will help me alot! =)

  82. If anyone’s having trouble with the code posted by Steve Andersen (Thank You!), it may be because of the following segment of code.

    while (clipParent != _root)

    {

    mat.concat(clipParent.transform.matrix);

    clipParent = clipParent._parent;

    }

    //There’s 2 of these.

    My mc’s using CollisionDetection were all located on _level1. It was easy to fix, just change “_root” to “_level1.” You may be able to fix it so it works on all levels, but I haven’t found the solution yet. Hope this helps someone.

  83. Thanks.

    After finally understanding the coding you used in the drag and drop sample I was able to use the codes in my game to provide me with a transparant game background that the game wouldn’t register as a hit. Also you had 90% of my work cut out for me as I was able to make the box invisible and run a few if commands to stop the player leaving the player area. Whilst not 100% perfect yet your tutorial has helped me a lot and will definatly be crediting you in it (even though i probably won’t be releasing -its a fun home project)

  84. Thank you very much Grant, and thank you very much Steve! Really great job, really very useful and brilliant!

  85. I’ve released a bug fixes that resolve problems with resize of Flash Projector and recognition of collisions.

    You can download it here:

    http://www.kalamun.org/news/Flash-AS2-Collision-Detection-Resize-Bug-Fixed

  86. As a matter of interest, a very similar collision detection method is used to detect pixel proximity of dynamic text objects in FatCloud, the Flash version of Wordle. You can see an example here:

    http://netlife.com.au/2009/06/28/wordle-your-wordpress-flash-based-tag-cloud-plugin/

    The source code is available in the “Developer Pack”.

  87. nice work kalamun,..but the intersection rectange is not calculate it ok

    i think here it needs some fix:

    intersection.x+=bounds.xMin;

    intersection.y+=bounds.yMin;

  88. First of all thank you very much for an excellent guide. But i have a big problem. My game works fine with your code, but when i upload it to the Internet it’s just not working! I mean the game works fine except for the Collision Detection that doesn’t. Plaes help me! Thank you in advance.

    (you can see the game at my URL.. i got planty there but the Packman is the problematic one.

  89. Thanks for that code, with the fullscreen correction is totally nice!

    Cheers

  90. FlashDeveloper March 22, 2010 at 6:22am

    Very Helpful

  91. Hello,

    Thank you very much for your code source, very help ^_-

    I also have a small problem that i don’t understand, why, when i load the test in another swf with x != 0 and y !=0 , the hitTest doesn’t work ?

    Thank’s for your answer and sorry for my english ^_-

    ++ Apf

  92. Hello !

    It’s possible with this :

    createEmptyMovieClip(“ligne”, 1);

    ligne.lineStyle(1, 0x000000, 100);

    ligne.moveTo(mc2._x, mc2._y);

    ligne.lineTo(_xmouse, _ymouse);

    Because :

    var collisionRect:Rectangle = CollisionDetection.checkForCollision(mc1,ligne,120);

    Doesn’t work !

    Collision with lineTo and mc1.

  93. im confused. if i wanted to use this, how would i go about doing that? how do i set up the code to work with my MCs?

  94. Hey Grant
    saw you at flash Israel and got really inspired! so thanks for that!
    then came here to find something I was always waiting for someone to solve, I hoped that Adobe would’ve discovered this need as I find it very obvious, like why hitTestObject can take only the bounding box? why can’t it do what you are doing here?
    this really puzzles me, I realize this post is very old, yet this issue is still current and relevant.
    I translated your class to AS3 and wanted to ask you, given that my intent is to use collision detection on a flash casual game that suppose to run on Android mobile devices, is it possible to bypass the use of BitmapData.draw() method? this is due to performance reasons…
    would really appreciate your comment
    cheers
    Yariv 🙂

  95. @Grant: Many thanks for sharing
    @Steve Anderson: You’re a life savier !

  96. For anybody still reading this: kalamun’s update fixes the problem, as long as you replace this:

    class CollisionDetection {

    with this:

    class com.gskinner.sprites.CollisionDetection {

    Thank you so much, guys!

  97. Hey Guys could smbdy post the fullscreen/resize bugfix here please!
    Thank you!
    Great class!

Leave a Reply

Your email address will not be published. Required fields are marked *