简体   繁体   English

Flash Actionscript +在碰撞时获取x和y坐标

[英]Flash Actionscript + get x & y coords on collision

I'm running a HitTestPoint to detect when two of my mc's collide, but I don't want them to be able to go right through each other. 我正在运行一个HitTestPoint来检测我的两个mc何时发生碰撞,但是我不希望它们能够彼此通过。 Someone suggested I find the x,y coords of the moving object when the collision occurs, then update that mc's coords to this x,y each time a collision occurs. 有人建议我在发生碰撞时找到运动对象的x,y坐标,然后在每次发生碰撞时将mc的坐标更新为该x,y。 I've been Googling but came up empty. 我去过谷歌搜索,但空了出来。 Below is my code and what I tried 下面是我的代码和我尝试过的

 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");
}
}

} }

You are nearly there. 你快到了。 When you do the in-circle check then you need to set the coin_mc co-ordinates. 当您进行圆内检查时,您需要设置coin_mc坐标。 What values you set it to depends on what you want to happen, eg stop, bounce off, disappear, etc for example the code below just stops the coin_mc at the point at which it touches: 您将其设置为什么值取决于您想要发生的事情,例如停止,反弹,消失等,例如下面的代码只是将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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM