I’m making a simple snake game in as3. the problem I’m having is that the stroke on elements slightly overlap thus when the snake passes by the apple in an adjacent square it triggers hit test object. If i reduce the size or stroke of either the snake or the apple then they dont properly line up to the grid. is there a way i can use hittestobject but reduce the area of the object that will trigger it. ie only trigger if it hits the center of the object?
//this will hit when snake is adjacent due to overlapping strokes
if(snake.hitTestPoint(apple.x,apple.y,true))
{
trace('hit');
}
//this doesnt work for some reason
if(snake.hitTestPoint(apple.x+2,apple.y+2,true))
{
trace('hit');
}
when I traced the values for the bottom code the values are what would be expected but some reason hitTestPoint wont trigger. strangely if I do + 1 it will still work but because the stroke is 2 i need at least +3. If i set apple.x+n as a variable and use the variable in hitTestPoint it wont trigger. also on all cases where it does not trigger i get no errors and all trace values are what you would suspect…
In the snake-movieclip, add another clip that functions as it’s center, then test for the center-clip instead of the whole snake-clip.
if(snake.center.hitTestPoint(apple.x,apple.y,true))
{
trace(‘hit’);
}
I’m guessing this is the easiest way to solve your problem.