我在Pong的一个简单的(或者我认为的)游戏中使用jQuery Collision。没有人工智能或在线互动,只有两个桨和一个或两个现实生活中的球员可以控制的球。jQuery碰撞插件|如何真正检查是否有碰撞
当试图对球进行动画处理并检查它是否与任何东西实际相撞时,我就会变短。我已经阅读了SourceForge上的文档(参见上面的链接),但是我仍然在如何实际检查对撞机是否实际触及任何东西方面有点失落。在我看来,这些文档可以使用一些例子。
JavaScript代码,我目前有:
$(document).ready(function()
{
var ball = $("#ball");
var topPaddle = $("topPaddle");
var bottomPaddle = $("bottomPaddle");
var topPaddleHits = $("#ball").collision("#topPaddle", { mode: "collision", obstacleData: "topOData" });
var bottomPaddleHits = $("#ball").collision("#bottomPaddle", { mode: "collision", obstacleData: "bottomOData" });
var anim = setInterval(function()
{
ball.css({ "left": "+=5", "top": "+=5" });
if (bottomPaddleHits.data("bottomOData") == bottomPaddle)
{
anim = clearInterval(anim);
alert("Bottom paddle hit!");
}
}, 50);
});
我也试过if (bottomPaddleHits == 1)
但这是没有好,无论是。
CSS的顶部/底部桨和球,如果它的问题:
#ball
{
width: 10px;
height: 10px;
position: absolute;
background: #FFF;
top: 200px;
left: 100px;
}
#topPaddle
{
position: absolute;
width: 300px;
height: 10px;
background: lime;
top: 100px;
left: 50px;
}
#bottomPaddle
{
position: absolute;
width: 300px;
height: 10px;
background: lime;
bottom: 100px;
left: 50px;
}
我只是不知道如何去检查,如果有什么东西竟然打与否。
。 – 2013-03-28 07:28:00
的jQuery 1.9.1和jQuery UI 1.10.2均来自谷歌的CDN工作, – Xenolithic 2013-03-28 07:31:13
很确定1.8以上的东西无法工作,因为API已经改变。 – 2013-03-28 07:33:47