# Hit Test

**URL:** <https://forum.kirupa.com/t/hit-test/330874>\
**Category:** Uncategorized\
**Created:** [April 1, 2013, 3:12am UTC](https://forum.kirupa.com/t/hit-test/330874 "2013-04-01T03:12:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![na\_il](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@na\_il](https://forum.kirupa.com/u/na_il)\
**Post date:** [April 1, 2013, 3:12am UTC](https://forum.kirupa.com/t/hit-test/330874/1 "2013-04-01T03:12:28Z")

</div>

I have a lattice of points I want each to listen for a hit-test and glow when hit how can i do this? I’m not sure how to get it to work for each individual point…I thought board.graphics would work but I guess not, please bear with me I’m not the best 🙂 Thanks!

Here is the Code:

```auto

var redRect:Rectangle = board.graphics.getBounds(this);
var redClipBmpData = new BitmapData(redRect.width, redRect.height, true, 0);
redClipBmpData.draw(board.graphics);

var blueRect:Rectangle = myPoint.getBounds(this);
var blueClipBmpData = new BitmapData(blueRect.width, blueRect.height, true, 0);
blueClipBmpData.draw(myPoint);

//Part I -- Set up the board

var bWidth:Number = stage.stageWidth;
var bHeight:Number = stage.stageHeight;

var grid:Number = 20; // Size of the grid and number of lattice points in each direction
var dotsWide:Number = Math.ceil(bWidth/grid) - 1;
var dotsHigh:Number = Math.ceil(bHeight/grid) - 1;

var board:Sprite = new Sprite();
//var myPoint:Sprite = new Sprite();
stage.addChild(board);

// Add a bunch of circles to represent lattice points
board.graphics.beginFill(0x000000);
for (var i=1; i<=dotsHigh; i++) {
    for (var j=1; j<=dotsWide; j++) {
        board.graphics.drawCircle(j*grid,i*grid,3);
    }
}
board.graphics.endFill();

addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
    if(redClipBmpData.hitTest(new Point(board.graphics.x, board.graphics.y),
                                255,
                                blueClipBmpData,
                                new Point(myPoint.x, myPoint.y),
                                255
                          ))
    {
        trace("hit");
        board.graphics.filters = [new GlowFilter()];
    }
    else
    {
        board.graphics.filters = [];
    }
}

// Part II -- Add drag and drop functionality

myPoint.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

function startMove(evt:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}

function pointMove(e:MouseEvent):void {
    myPoint.x = goodX(board.mouseX);
    myPoint.y = goodY(board.mouseY);
    e.updateAfterEvent();
}

stage.addEventListener(MouseEvent.MOUSE_UP, stopAll);

function stopAll(e:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMove);
}

//Part III -- Helper functions to stay within boundary AND snap to grid

function goodX(inX:Number):Number {
    if (inX > grid*dotsWide) {
        return grid*dotsWide;
    }
    if (inX < grid) {
        return grid;
    }
    return grid*Math.round(inX/grid);
}
function goodY(inY:Number):Number {
    if (inY > grid*dotsHigh) {
        return grid*dotsHigh;
    }
    if (inY < grid) {
        return grid;
    }
    return grid*Math.round(inY/grid);
}

```
