1

I have a grid of objects that have been added to the stage dynamically, the objects are either of a Class Red or Class Blue, how do I perform a hit test on the Classes? I want the Blue object to disappear once it hits a Red one. Help please, if you have a video or a link to a tutorial that would be appreciated. Thanks.

Garth Humphreys
  • 793
  • 2
  • 9
  • 16
  • Did one of the answers below work out for you? –  Jul 23 '11 at 01:57
  • Why can't you just use hitTestObject? You may need to post some code to clarify what you mean by "dynamically" because, well, I would normally say that if you want to do a hit test between two objects then use hitTest – jhocking Jul 26 '11 at 17:40

3 Answers3

3

You should Use the Collision Detection Kit

http://code.google.com/p/collisiondetectionkit/

papachan
  • 1,891
  • 17
  • 20
3

Here is a link to a tutorial, and below an excerpt from that tutorial that shows the basic principle:

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(circle_mc.hitTestObject(rect_mc))
       {
           output_txt.text = "HIT"
       }
       else
       {
           output_txt.text = "MISS"
       }
}

http://www.designscripting.com/2011/05/hittest-as3-for-collision-detection-flash-actionscript-3/

What you're going to want to do is perhaps embed that code above inside your Blue class, and then basically check and see if you hit any object on the stage, and then check to ensure it's type is type Red or whatever you want, then do whatever you want based on the result:

import flash.events.Event;
import mypackage.Red;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(stage){
        var i:int = 0;
        for(i; i < stage.numChildren; ++i){
            if(this.hitTestObject(stage.getChildAt(i) && stage.getChildAt(i) != this){
                if(stage.getChildAt(i) is Red){
                   //This object has collided with a Red object
                }
            }
        }
    }        
}

I just slapped that code together off the top of my head but if it doesn't work "out of the box" then just use the basic principle and modify it accordingly. Also, having many objects all scanning all children of the stage at the same time on every frame will eventually be a real intensive task and lag your app. What would be more preferable is to have a single enter_frame event somewhere with two nested for loops checking all children to see who collides with who, or even better a linked-list structure for Red/Blue objects and use a linked list loop to check collision.

  • Thanks, I think this is the same thing as I stated above, I what to do a hit test on objects that have been added to the stage dynamically. – Garth Humphreys Jul 23 '11 at 00:31
  • Sorry, I just saw what you wrote, I'll give the second part a try, thanks again! – Garth Humphreys Jul 23 '11 at 00:33
  • This code will get all objects that are on the stage at the time that the loop runs. So if you've dynamically added objects, the next time the loop runs, those objects will be captured. This is because the loop runs through every direct child of the stage (stage.getChildAt(i)) based on the number of children added, which is incremented every time an object is added either statically or dynamically. –  Jul 23 '11 at 00:34
  • The bottom code won't work with stage in the class file and should be replaced with parent – Koden Oct 16 '20 at 20:46
1

http://www.foundation-flash.com/tutorials/as3hittesting/

apscience
  • 7,033
  • 11
  • 55
  • 89
  • Thanks @gladoscc but I think this only works with objects that are already on the stage with a instance name. I what to do a hit test on objects that have been added to the stage dynamically. – Garth Humphreys Jul 23 '11 at 00:29
  • You can have a array of Reds and another array for Blues, then loop through one array and test another. That's what I would do. – apscience Jul 24 '11 at 10:39