2014-02-23 51 views
2

我想檢測2個矩形之間的碰撞,所以我使用矩形交集方法來確定碰撞發生的位置。處理碰撞的問題(代碼不工作?)

enter image description here

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

您需要更改weakRect1(影片剪輯)不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; 
+0

是位置!我試圖在移動MovieClip時移動一個矩形。謝謝! – user3307694