我正在尝试检测2个矩形之间的碰撞,因此我使用矩形交叉方法来确定碰撞发生的位置。
if (weakRect.y < strongRect.y)
{
weakRect.y -= overlapRect.height; << Does nothing
vSpeed = 0; << Changes int value
}
当我运行它时,这行代码似乎没有做任何事情,我已经尝试删除它并添加其他东西但它似乎不起作用,尽管if语句中的其他内容工作。
function rectBlock(strongRect1:MovieClip, weakRect1:MovieClip){
var strongRect:Rectangle = new Rectangle;
var weakRect:Rectangle = new Rectangle;
strongRect = strongRect1.getBounds(stage);
weakRect = weakRect1.getBounds(stage);
var overlapRect:Rectangle = strongRect.intersection(weakRect);
trace (overlapRect)
if (overlapRect.width > overlapRect.height)
{
if (weakRect.y < strongRect.y)
{
weakRect.y -= overlapRect.height;
vSpeed = 0;
}
else if (weakRect.y > strongRect.y)
{
weakRect.y += overlapRect.height;
vSpeed = 0;
}
}
if (overlapRect.width < overlapRect.height)
{
if (weakRect.x < strongRect.x)
{
weakRect.x -= overlapRect.width;
hSpeed = 0;
}
else if (weakRect.x < strongRect.x)
{
weakRect.x += overlapRect.width;
hSpeed = 0;
}
}
即使在if语句下跟踪“是”时,它也会在碰撞时打印出来,但它不会将weakRect移回碰撞中。
答案 0 :(得分:0)
您需要更改weakRect1(movieclip)的位置而不是weakRect(矩形)
weakRect1.y -= overlapRect.height; //<=>weakRect.y -= overlapRect.height;
...
weakRect1.y += overlapRect.height; //<=>weakRect.y += overlapRect.height;
...
weakRect1.x -= overlapRect.width; //<=>weakRect.x -= overlapRect.width;
...
weakRect1.x += overlapRect.width; //<=>weakRect.x += overlapRect.width;