# Listening for Collision

**URL:** https://forum.kirupa.com/t/listening-for-collision/277057
**Category:** flash
**Created:** [December 7, 2008, 1:31am UTC](https://forum.kirupa.com/t/listening-for-collision/277057 "2008-12-07T01:31:39Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Pherank](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@Pherank](https://forum.kirupa.com/u/Pherank)
#### Post date: [December 7, 2008, 1:31am UTC](https://forum.kirupa.com/t/listening-for-collision/277057/1 "2008-12-07T01:31:39Z")

</div>

What I’m trying to figure out is this: I have 5 card objects that can be dragged onto 3 target objects, and when the user is dragging a card over any target I would like to play an animation for that target (as a visual cue). The basic, stripped down code looks like this -

```auto

package {
    public class Drag_and_Drop extends MovieClip {

        private var activeCard_mc:MovieClip;
        private var target_mc:MovieClip;
        private var dragging:Boolean = false;

        public function Drag_and_Drop() {

            // Attach draggable cards to stage
            for (var i:Number=numberOfCards; i>0; i--) {
                activeCard_mc = new ActiveCard;
                activeCard_mc.name = "activeCard_" + i;
                activeCard_mc.mouseChildren = false;// Keep activeCard_mc textfield from interfering
                addChild(activeCard_mc);
            }
            // Attach targets to stage
            for (var n:Number=0; n<3; n++) {
                target_mc = new Target;
                target_mc.name = "target_" + (n + 1);
                target_mc.addEventListener(Event.ENTER_FRAME, targetEnterFrame);
                addChild(target_mc);
            }
        }
        public function targetEnterFrame(event:Event) {
            if (dragging) {
                //trace(dragging);
                if (target_mc.hitTestObject(activeCard_mc)) {
                    target_mc.gotoAndPlay(2);
                }
            } else {
                //trace(dragging);
                target_mc.gotoAndStop(1);
            }
        }
        public function dragCard(event:MouseEvent):void {

            dragging = true;

            event.target.startDrag(false, rectangle);
            event.target.parent.addChild(event.target);
        }
        public function stopDragCard(event:MouseEvent):void {
            event.target.stopDrag();

            dragging = false;
        }
    }
}

```

I’m not sure if the ENTER\_FRAME listener is the best technique here. Also, the hitTest doesn’t seem to do anything (even though there’s no error):

```auto
target_mc.hitTestObject(activeCard_mc)

```
