以下是我到目前为止我的计时器对象如何添加到舞台:
//create new movie clip
var newBox = new mcBox();
//add to stage
stage.addChild(newBox);
//add to array
aBoxesArray.push(newBox);
for (var i:uint = 0; i < aBoxesArray.length; i++)
{
//add the boxes to sizes array
aSizesArray.push(aBoxesArray[i].width);
//add box as movieclip so frame lables can work and we can get current e target
for each(newBox in aBoxesArray)
{
newBox.addEventListener(MouseEvent.CLICK, onBoxClicked, false, 0, true);
}
}
这是我要在我的ENTER_FRAME事件监听器中处理逻辑的函数:
private function checkBoxesOverlapping():void
{
for (var i:int = 0; i < aBoxesArray.length; i++)
{
var currentBox:mcBox = aBoxesArray[i];
if (currentBox.hitTestObject())
{
trace("OVERLAPPING");
}
}
}
我只是不太确定要在hitTestObject中添加什么或者如何真正去做这件事。如果有人能够指出正确的方向,甚至给我线索,那就不仅仅是感恩了!谢谢大家!
答案 0 :(得分:2)
另一种方式: 您还可以通过计算对象的距离来实现此目的。
function distance(p1, p2) {
var dist, dx, dy: Number;
dx = p2.x - p1.x;
dy = p2.y - p1.y;
dist = Math.sqrt(dx * dx + dy * dy);
//Obj boundaries test
//Registraion point of the MovieClip must be center of the object.
if (dist < (p1.width / 2 + p2.width / 2)) {
//trace(p1.name, p2.name);
}
}
重叠测试
function overlapTest(): void {
for (i = 0; i < objs.length; i++) {
var target: MovieClip = MovieClip(objs[i]);
for (var j: uint = 0; j < objs.length; j++) {
if (objs[j] != target) {
distance(target, objs[j]);
}
}
}
}
答案 1 :(得分:1)
添加另一个循环:
private function checkBoxesOverlapping():void
{
for (var i:int = 0; i < aBoxesArray.length; i++)
{
var currentBox:mcBox = aBoxesArray[i];
for (var j:int = 0; j < aBoxesArray.length; j++)
{
var box:mcBox = aBoxesArray[j];
if (currentBox != box && currentBox.hitTestObject(box))
{
trace("OVERLAPPING");
}
}
}
}