Phaser - 如何检查在其他两个精灵之间移动的精灵?
Phaser - How to check for sprite moving between two other sprites?
我正在尝试检查一个 sprite 是否正在通过其他两个 sprite 之间的线。我试图检查精灵是否与 Phaser.Line 重叠:
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);
只要我的 sprite 不是 on/over 行,查看控制台就会计算错误事件。将它移到线上有时会给我真实的事件,但并非总是如此。看起来帧变化太快而无法检测到它。似乎也不可能对行进行重叠检查。
我也尝试过:
this.GreenToRedArray = this.lineGreenToRed.coordinatesOnLine();
并在 update() 中:
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);
}
},
只要我在线上移动得相当慢,它就可以工作,但一旦移动速度稍快一点,它就不再捕捉到它了。
关于如何检查一个 sprite 是否越过其他两个 sprite 之间的线的任何提示?
可以把它想象成两个球标记一个目标,第三个球在这两个球之间射门。
非常感谢您的帮助。
我没有太多地使用 Phaser.Line
s,所以经过一些研究,根据我在其他地方看到的,我可能会建议稍微改变一下。
一种选择是将球门线更改为 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
,并检查交叉点。
我正在尝试检查一个 sprite 是否正在通过其他两个 sprite 之间的线。我试图检查精灵是否与 Phaser.Line 重叠:
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);
只要我的 sprite 不是 on/over 行,查看控制台就会计算错误事件。将它移到线上有时会给我真实的事件,但并非总是如此。看起来帧变化太快而无法检测到它。似乎也不可能对行进行重叠检查。
我也尝试过:
this.GreenToRedArray = this.lineGreenToRed.coordinatesOnLine();
并在 update() 中:
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);
}
},
只要我在线上移动得相当慢,它就可以工作,但一旦移动速度稍快一点,它就不再捕捉到它了。
关于如何检查一个 sprite 是否越过其他两个 sprite 之间的线的任何提示?
可以把它想象成两个球标记一个目标,第三个球在这两个球之间射门。
非常感谢您的帮助。
我没有太多地使用 Phaser.Line
s,所以经过一些研究,根据我在其他地方看到的,我可能会建议稍微改变一下。
一种选择是将球门线更改为 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
,并检查交叉点。