我知道这个问题已经被问了很多次,我也看到了他们在SO和其他论坛上的解决方案。解决方案建议的大多数时间是使用Web Workers。一起运行多个jquery函数
我正在开发的游戏需要我同时运行多个功能。其中一个是click
功能,另一个是setInterval
。
我的做法可以参见here in this JSFiddle。 (继续点击灰色区域让玩家跳跃)。
整个想法是在1000ms间隔后不断产生蓝色障碍物。
在我先前的方法中,只有当我点击使球员跳跃时才会产生障碍,否则他们不会如预期那样。
我如何才能并行运行这样两种功能方面实现 的产卵障碍的目的,同时也使玩家的跳跃。
其次,考虑到游戏开发实施这个过程 最好的方法是什么,即达到一定的效率水平以便动画不受影响。
下面是HTML和Javascript代码,我一直在努力:
<div class="container">
<div class="player"></div>
<div class="obstacle-container">
<div class="obstacle"></div>
</div>
</div>
$.fn.animator = function() {
var hit_list, done = false;
$(".container").click(function() {
if (!done) {
$(".obstacle").stop().animate({
left: "-=105%"
}, 10000, "linear");
$(".player").stop().animate({
bottom: "+=100px"
}, {
duration: 300,
complete: function() {
$(".player").animate({
bottom: "0"
}, 800);
},
step: function() {
//Test for collision
hit_list = $(".player").collision(".obstacle");
if (hit_list.length !== 0) {
$(function() {
if (!done) {
$(".container").append("Game Over!");
return false;
}
});
done = true;
}
}
});
}
});
};
$(function() {
$('.container').animator();
});
var interval = null;
$(".obstacle-container").obstacle_generator();
$.fn.obstacle_generator = function() {
interval = setInterval(function() {
$(".obstacle-container").append('<div class="obstacle"></div>');
}, 1000);
};
谢谢你指点我正确的方向。我发现http://gameprogrammingpatterns.com/game-loop.html,我认为这是继续前进的道路。 – md1hunox