2011-01-16 11 views
0

私はHitTestPointを実行して、2つのMCが衝突したときに検出しますが、それらが互いに右に進むことを望んでいません。誰かが、衝突が発生したときに動く物体のx、y座標を見つけ、衝突が起こる度にそのm、co座標をこのx、yに更新するよう提案しました。私はグーグルで行ってきたが空になった。以下は私のコードと私はあなたがほとんど存在しFlash Actionscript +衝突時に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

を試してみましたです。サークル内のチェックを行うときは、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"); 
} 
関連する問題