Hi everyone. I was wondering if anyone could help me with a little collision detection. Basically, I have a grid set up. The grid’s tiles have a gravity variable, so they all fall. However, I don’t want them to. I want them to check to see if there are tiles beneath them, so they stop falling when they hit.
Some sample code:
var tileClasses:Array = [ Blue, Green, Black, Yellow, Red ];
var grid:Array = new Array( 9 );
var i:int = -1;
var spacing:Number = 3;
var gravity:Number = .025;
var vy:Number = 0;
while ( ++i < 9 ) {
var column:Array = new Array( 9 );
grid[ i ] = column;
var j:int = -1;
while ( ++j < 9 ) {
var randomClass:Class = tileClasses[ Math.floor( Math.random() * tileClasses.length ) ];
var tile:DisplayObject = addChild( new randomClass() );
MovieClip( tile ).markedForRemoval = false;
//all of your positioning/event listening as was
tile.x = (i*((tile.width)+spacing)+50);
tile.y = (j*((tile.height)+spacing)+50);
this.addChild(tile);
tile.alpha = .35;
tile.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
tile.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
tile.addEventListener(MouseEvent.CLICK, clickHandler);
tile.addEventListener(Event.ENTER_FRAME, enterHandler);
column[ j ] = tile;
}
}
Here is my enterHandler method:
function enterHandler(event:Event):void {
var tile:Object = event.target;
vy += gravity;
event.target.y += vy;
if (event.target.y + event.target.height / 2 > stage.stageHeight) {
event.target.y = stage.stageHeight - event.target.height / 2;
trace("hit the ground");
}
}
}
Right now I have them falling and they hit the bottom of the stage perfectly. I was more just using that to test my gravity. What I really want is, like I said, to have the tiles stop falling when they hit the tiles below. I’ve tried using a hitTestObject, and I’m sure that’s what I need to do, but I couldn’t get it to work properly.
Can anyone lend a hand?