2017-04-21 120 views
0

我試圖檢查一個精靈是否通過其他兩個精靈之間的線。我試圖檢查精靈與重疊Phaser.Line:Phaser - 如何檢查在兩個其他精靈之間移動的精靈?

this.lineGreenToRed = new Phaser.Line(this.ballGreen.x, this.ballGreen.y, this.ballRed.x, this.ballRed.y); 

this.checkPassed = this.lineGreenToRed.pointOnLine(this.ballBlue.position.x, this.ballBlue.position.y); 
if (this.checkPassed) { 
    console.log('GreenToRed passed'); 
    //counter++; 
}; 
console.log(this.checkPassed); 

展望臺這個計數的假事件,每當我的精靈不在/過線。將它移到線上有時會給我真實的事件,但並非總是如此。它看起來像框架變化太快,無法檢測到它。在線上重疊檢查似乎也不可能。

我也曾嘗試用:

this.GreenToRedArray = this.lineGreenToRed.coordinatesOnLine(); 

和更新():

this.GreenToRedArray.forEach(this.checkPoint, this); 

然後:

checkPoint : function(element){ 
     if (this.ballBlue.position.x == element[0] && this.ballBlue.position.y == element[1]){ 
     console.log('GreenToRed passed'); 
     this.score++; 
     console.log(this.score); 
     this.scoreText.setText(this.score); 
    } 
}, 

這工作只要我謹很慢了該線路,但只要事情移動得更快一點,它就不會再接受它了。

任何提示我如何檢查一個精靈是否正在兩個其他精靈之間的線上移動?

把它想象成兩個球,標誌着一種進球和第三球在這兩者之間通過射門。

非常感謝您的幫助。

回答

1

我還沒有太多的使用Phaser.Line,所以經過一些研究以及從我在其他地方看到的情況,我可能會建議對此稍有不同。

一個選項是將球門線改爲Phaser.Sprite,然後檢查overlap

或者,您可以檢查矩形相交,如Overlap Without Physics示例中所示。

function update() { 
    if (checkOverlap(this.ballBlue, this.goal)) { 
     console.log('overlapping'); 
    } 
} 
function checkOverlap(spriteA, spriteB) { 
    var boundsA = spriteA.getBounds(); 
    var boundsB = spriteB.getBounds(); 

    return Phaser.Rectangle.intersects(boundsA, boundsB); 
} 

這可能進行調整,所以如果你不想使用Sprite,你可以使用一個Phaser.Rectangle代替Phaser.Line,改變相應的功能。

我想你也可以從球的前一個位置到當前位置繪製另一個Phaser.Line,然後檢查一個十字路口。

+0

非常感謝您的幫助 - 我的問題是「目標」不能解決。在比賽中我有3個球在移動,從彼此和世界的邊界反彈。所以球門線/精靈/長方形需要根據兩個球的距離和角度作爲球門柱進行計算,每輪新一輪。這也是爲什麼從前一個位置開始畫第二條線不起作用的原因:球通過球門線後可能會處於完全不同的位置(與終點不相交)。我能以某種方式創建靈活的精靈或矩形嗎? –

+0

我需要的是更像矩形(或精靈)從x1,y1到x2,y2而不是給定的recangle x,y,height,width。任何進一步的提示/想法將不勝感激:-) –

+0

啊,我現在明白了。我沒有意識到兩個球門也在移動。深入挖掘,它看起來像[Phaser.Line.intersectsRectangle](http://phaser.io/docs/2.6.2/Phaser.Line.html#.intersectsRectangle)可能會有所幫助。有一個例子在http://www.html5gamedevs.com/topic/27412-line-collision/?do = findComment&comment = 157389不同於'pointOnLine',它只是檢查'Sprite'的錨點,它將測試你的球精靈的整個範圍。 –