简体   繁体   English

如何在使用setInterval循环的同时使用HTML画布和javascript来定义局部变量?

[英]How to define local variable once using HTML canvas with javascript while using a setInterval loop?

I am programming a simple game using the HTML canvas and editing it with JavaScript. 我正在使用HTML画布编程一个简单的游戏,并使用JavaScript对其进行编辑。 I several functions which draw something on the canvas, and all those functions are looping using a single setInterval. 我有几个在画布上绘制内容的函数,所有这些函数都使用单个setInterval循环。 However some of these functions need to have a local variable that contains a certain attribute about that function (to be precise, a toggle, whether a function is "xx" or not), and as there will be many of these functions, storing this variable in the global window is very unpractical. 但是,其中一些功能需要具有包含有关该功能的特定属性的局部变量(准确地说,是一个切换,无论一个功能是否为“ xx”),并且由于会有很多这些功能存储在此变量中全局窗口中的变量非常不切实际。

Is there a way of defining the variable (or attribute) in this function without this variable being reset every loop? 有没有一种方法可以在此函数中定义变量(或属性),而不必在每次循环时都重置该变量?

So in code I have: 所以在代码中我有:

var DoSomething = function(){
var xx = new Boolean
[...]
if (condition) { xx = false}
if (condition) { xx = true}
}

And all is executed in the main loop: 所有操作都在主循环中执行:

var gameLoop = function(){
DoSomething()
OtherFunctions()
}

var startGame = function(){
setInterval(gameLoop,10)
}

startGame()

Like I said, defining xx in the global window would be very unpractical as there are many function with this xx attribute/variable. 就像我说的那样,在全局窗口中定义xx是非常不切实际的,因为此xx属性/变量有很多功能。 But right now, the loop keeps resetting xx at the start of the loop. 但是现在,循环会在循环开始时继续重置xx Is there a way of defining the local variable here, without it being reset with every loop of the setInterval? 有没有一种在这里定义局部变量的方法,而不必在setInterval的每个循环中都将其重置?

By wrapping your code inside a function, everything inside that function becomes hidden from the rest of the world. 通过将代码包装在一个函数中,该函数中的所有内容都对世界其他地方隐藏了。 So now you can declare variables outside your functions, and they will remain private and only accessible by your program. 因此,现在您可以在函数外部声明变量,它们将保持私有状态,并且只能由程序访问。

(function() {
    var xx = new Boolean,
        loop;

    var DoSomething = function() {
      // xx will now keep it's state
        if (condition) { xx = false; }
    }

    var gameLoop = function() {
        DoSomething();
        OtherFunctions();
    }

    var startGame = function() {
        loop = setInterval(gameLoop, 10);
    }
}());

The extra parenthesis at the end makes the function call itself immediately, so as soon as the script is loaded the function will run 最后的括号使函数立即调用自身,因此,一旦脚本被加载,函数将运行

e.g function() {<code>}()

I recommend reading about prototypical inheritance and using it to create classes to represent objects in your game. 我建议阅读有关原型继承的知识,并使用它来创建代表游戏中对象的类。 It's a good way to get started with OOP in javascript. 这是开始使用JavaScript进行OOP的好方法。

https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript https://developer.mozilla.org/zh-CN/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript

Also read up on closures to better understand how the above example works. 还请阅读闭包以更好地理解上述示例的工作原理。 How do JavaScript closures work? JavaScript闭包如何工作?

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

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