我正在运行一个HitTestPoint来检测我的两个mc是否发生碰撞,但我不希望它们能够直接通过彼此。有人建议我在发生碰撞时找到移动物体的x,y坐标,然后每次发生碰撞时将mc的坐标更新为此x,y。我一直在谷歌搜索但是空了。下面是我的代码和我尝试的内容
private function __checkHit($evt:Event):void {
if (this.coin_mc.hitTestObject(target)) {
if (!hitting) {
coinSnd.play();
count++;
total_count.text = String("$" + count);
hitting = !hitting;
}
} else {
hitting = false;
}
if (mug_bounds.hitTestPoint(coin_mc.x,coin_mc.y, false))
{
// do our in-circle check
if((mug_bounds.x - coin_mc.x) * 2 + (mug_bounds.y - coin_mc.y) * 2 <= (mug_bounds.width/2 + coin_mc.width/2) * 2)
{
**var coinX:Number = coin_mc.x;
var coinY:Number = coin_mc.y;
trace(coin_mc.x);
trace(coin_mc.y);
coin_mc.x = coinX;
coin_mc.y=coinY;**
}
}
else
{
trace("Didn't Hit Mug");
}
}
}
答案 0 :(得分:0)
你快到了。当您进行圈内检查时,您需要设置coin_mc坐标。你设置它的值取决于你想要发生什么,例如停止,反弹,消失等等,例如下面的代码只是在它接触的点停止coin_mc:
if (mug_bounds.hitTestPoint(coin_mc.x,coin_mc.y, false))
{
// do our in-circle check
if((mug_bounds.x - coin_mc.x) * 2 + (mug_bounds.y - coin_mc.y) * 2 <= (mug_bounds.width/2 + coin_mc.width/2) * 2)
{
//first find the angle between the two centre points
var diffX:Number =(coin_mc.x-mug_bounds.x);
var diffY:Number =(coin_mc.y-mug_bounds.y);
var radii:Number = mug_bounds.width/2 + coin_mc.width/2;
var angle:Number = Math.atan2(diffX,diffY)
// use trig to calculate the new x position so that the coin_mc isn't touching the mug
coin_mc.x = mug_bounds.x + radii*Math.sin(angle);
coin_mc.y = mug_bounds.y + radii*Math.cos(angle);
}
} else {
trace("Didn't Hit Mug");
}