2017-02-12 66 views
0

我正在嘗試編寫一個函數,檢查在某個偏移量處重疊的街機物理體。這是我的代碼:檢查重疊在偏移處 - Phaser

function overlapAtOffsetSprite(object1, object2, offsetX, offsetY) 
{ 

    if (typeof(object1.body) === "undefined" || typeof(object2.body) === "undefined"){ 
     return false; 
    } 

    var bounds1 = new Phaser.Rectangle(object1.position.x + object1.body.offset.x + offsetX, 
             object1.position.y + object1.body.offset.y + offsetY, 
             object1.body.width, object1.body.height); 
    var bounds2 = new Phaser.Rectangle(object2.position.x + object2.body.offset.x, object2.position.y + 
             object2.body.offset.y, object2.body.width, object2.body.height); 
    return Phaser.Rectangle.intersects(bounds1, bounds2); 

} 

function overlapAtOffset(object1, object2, offsetX, offsetY) 
{ 

    if (object1.name == "group") 
    { 
     object1.forEach(function(child) 
     { 
      if (overlapAtOffset(child, object2, offsetX, offsetY)) 
       return true; 
     }); 
    } 
    else if (object2.name == "group") 
    { 
     object2.forEach(function(child) 
     { 
      if (overlapAtOffset(object1, child, offsetX, offsetY)) 
       return true; 
     }); 
    } 
    else 
    { 
     return overlapAtOffsetSprite(object1, object2, offsetX, offsetY); 
    } 
    return false; 
} 

基本上函數overlapAtOffsetSprite檢查在偏移和功能overlapAtOffset檢查2個精靈之間的重疊爲通過每個組的精靈循環和使用overlapAtOffsetSprite一個精靈和一組之間或2個之間的重疊在每個精靈上。 overlapAtOffsetSprite功能似乎在測試時工作正常,但overlapAtOffset出現問題。

在此先感謝。

回答

0

哦!經過幾小時的盯着看,我發現了這個問題。我認爲forEach函數的return聲明是overlapAtOffset的聲明。因此,爲了解決這個問題,我使用了for循環,而不是使用group.children數組的forEach函數。我還編輯了overlapAtOffsetSprite來計算精靈的錨點。下面是任何有興趣的代碼:

function overlapAtOffsetSprite(object1, object2, offsetX, offsetY) 
{ 
    if (typeof(object1.body) === "undefined" || typeof(object2.body) === "undefined"){ 
     return false; 
    } 

    var bounds1 = new Phaser.Rectangle(object1.position.x + object1.body.offset.x - 
             object1.anchor.x * object1.width/object1.scale.x + 
             offsetX, object1.position.y + object1.body.offset.y - 
             object1.anchor.y * object1.height/object1.scale.y + 
             offsetY, object1.body.width, object1.body.height); 
    var bounds2 = new Phaser.Rectangle(object2.position.x + object2.body.offset.x - 
             object2.anchor.x * object2.width/object2.scale.x, 
             object2.position.y + object2.body.offset.y - 
             object2.anchor.y * object2.height/object1.scale.y, 
             object2.body.width, object2.body.height); 
    return Phaser.Rectangle.intersects(bounds1, bounds2); 

} 

function overlapAtOffset(object1, object2, offsetX, offsetY) 
{ 

    if (object1.name == "group") 
    { 
     for (var i = 0; i < object1.children.length; i++) 
     { 
      if (overlapAtOffset(object1.children[i], object2, offsetX, offsetY)) 
       return true; 
     } 
    } 
    else if (object2.name == "group") 
    { 
     for (var i = 0; i < object2.children.length; i++) 
     { 
      if (overlapAtOffset(object1, object2.children[i], offsetX, offsetY)) 
       return true; 
     } 
    } 
    else 
    { 
     return overlapAtOffsetSprite(object1, object2, offsetX, offsetY); 
    } 
    return false; 
} 

編輯: 顯然,我對自己說,但是,我仍然知道有人可能會發現在未來,這是有用的。檢查對象是否爲組的一種更好的方法是使用:object1.physicsType == Phaser.GROUP而不是object1.name == "group"