简体   繁体   English

一起运行多个jquery函数

[英]Running multiple jquery functions together

I know this question has been asked a lot of times, and I have seen the solutions to them on SO as well as other forums. 我知道这个问题已经问过很多次了,我已经在SO以及其他论坛上看到了针对他们的解决方案。 Most of the times the solution suggested is to use Web Workers . 建议的解决方案多数时候是使用Web Workers

A game I'm developing requires me to run multiple functions at the same time. 我正在开发的游戏要求我同时运行多个功能。 One of them is an on click function and other is a setInterval . 其中之一是上click功能等是setInterval
My approach at doing this can be seen here in this JSFiddle . 在JSFiddle中可以看到我执行此操作的方法。 (keep clicking in gray area to make player jump). (继续单击灰色区域可使玩家跳动)。

The whole idea is to continuously spawn those blue obstacles after an interval of 1000ms. 整个想法是在间隔1000毫秒后连续产生这些蓝色障碍。

In my earlier approach the obstacles would spawn only when I click to make player jump, otherwise they wouldn't as expected. 在我以前的方法中,障碍只有在我单击以使玩家跳动时才会产生,否则它们将不会达到预期。


  1. How can I run such two functions side by side in order to achieve the aim of spawning obstacles while also making player jump. 如何同时运行这两个功能,以达到产生障碍物同时又使玩家跳跃的目的。

  2. Secondly, what would be the best approach to carry out this process in view of game development ie attaining a certain level of efficiency so that the animations are not affected. 其次,考虑到游戏开发,即达到一定的效率水平,使动画不受影响,什么是执行此过程的最佳方法?


Here is the HTML and Javascript code I've been working on: 这是我一直在努力的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); }; 

The generic concept you want to investigate is known as a game loop . 您要研究的通用概念称为游戏循环

Almost every game will be built using some variant of this system: 几乎每个游戏都将使用此系统的某些变体来构建:

  • Initialise game 初始化游戏
  • Loop: 环:
    • Check for user input 检查用户输入
    • Update any actors 更新任何演员
    • Draw the scene 绘制场景
    • Wait until it's time to repeat 等到该重复了

A game running at 60 frames per second would perform this loop 60 times per second, or about once every 16ms. 以每秒60帧的速度运行的游戏每秒将执行60次此循环,或大约每16毫秒执行一次。

Compared to your original question, you wouldn't need to be running multiple execution threads (running multiple functions together) to achieve this. 与原始问题相比,您无需运行多个执行线程(一起运行多个功能)即可实现这一目标。

You are, in a way, already using a similar loop. 在某种程度上,您已经在使用类似的循环。 jQuery maintains its own loop for updating animations. jQuery维护自己的循环来更新动画。 Where you are checking for collisions as part of your animation step, this is the sort of thing you would do in a hypothetical Player.update() method. 在动画步骤中要检查碰撞的地方,这是在假设的Player.update()方法中要做的事情。 You want to move this code out of jQuery, and in to a loop that you control. 您想将此代码移出jQuery,并移入您控制的循环中。

Since you're running in a browser, the generic game loop becomes a bit more simple: 由于您正在浏览器中运行,因此通用游戏循环变得更加简单:

  • Check for user input - this can still be handled by event handlers, jQuery or not. 检查用户输入 -仍然可以通过事件处理程序(无论是否为 jQuery)进行处理。 Rather than directly changing properties like CSS position, though, they should act upon the state of the game object . 但是,它们应该直接作用于游戏对象的状态,而不是直接更改CSS位置之类的属性 For example, by changing the velocity of a Player object. 例如,通过更改Player对象的velocity

  • Update any actors - the important part of your loop. 更新任何参与者 -循环的重要部分。 You should check how many milliseconds have passed since you last looped, since the browser doesn't guarantee that your code will be run exactly, or at least, 60 times per second. 您应该检查自从上次循环以来经过了几毫秒,因为浏览器无法保证您的代码将每秒至少准确运行60次,或者至少每秒运行60次。 You should then loop through all of your game objects and update them all. 然后,您应该遍历所有游戏对象并更新它们。 In your Player.update() method, you would want to move it according to its velocity and the time passed, for example. 例如,在Player.update()方法中,您想根据其速度和经过的时间来移动它。

  • Draw the scene - if you're using DOM elements, then the browser handles drawing for you, of course. 绘制场景 -如果您使用的是DOM元素,那么浏览器当然会为您处理绘制。 If you were using a <canvas> element, then you would do drawing yourself as part of the loop here. 如果您使用的是<canvas>元素,则可以将自己绘制为此处循环的一部分。

  • Wait until it's time to repeat - this will be up to the browser to do for you, as part of normal setInterval / setTimeout behavior. 等待,直到需要重复时为止 -这是浏览器的工作,这是常规setInterval / setTimeout行为的一部分。

A simple game loop in JavaScript can look like this: JavaScript中的一个简单游戏循环如下所示:

var gameObjects = [];
// Initialise game, create player objects etc, add them to the array

var gameLoop = function() {
    // Loop through gameObjects, and call their respective update methods
};

setInterval(gameLoop, 16); // Try to run the loop 60 times per second.

In a complex game, you wouldn't have just a basic array to hold all game objects, this is just an basic example. 在复杂的游戏中,您将不会只有一个基本数组来容纳所有游戏对象,这只是一个基本示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM