CreateJS碰撞检测

时间:2014-08-20 16:45:25

标签: javascript createjs

我在设置碰撞检测时遇到问题。我已经关注了这个论坛和网络上的一些帖子,但似乎无法让它发挥作用。我正在尝试使用位于函数中的方形边框方法。

Square是使用用户输入在画布上移动的形状,而impactSquare是我试图测试其影响的固定形状。我似乎无法弄清楚哪个部分导致它无法工作。

//square bounding box
if (impactSquare.x < square.x + square.width &&
    impactSquare.x + impactSquare.width > square.x &&
    impactSquare.y < square.y + square.height &&
    impactSquare.height + impactSquare.y > square.y) {
    // collision detected!
    //square.graphics.beginFill("#F4F4F4");
    console.log("Collision Detected");
} else {
    // no collision
    //square.graphics.beginFill("#FF0000");
    console.log("No Collision"); 
}
//stage.update();

感谢您提供的帮助!

1 个答案:

答案 0 :(得分:1)

您可能遇到问题,因为您正在尝试使用匹配完美的hitTest。

在这里,使用它:https://github.com/olsn/Collision-Detection-for-EaselJS就像一个魅力。

如果交叉点是空的,那是因为没有碰撞。

if (intersection !== null) {
   //  hit
} 

干杯,

相关问题