我如何创建一个语法上会自动调用的函数?

问题描述 投票:-1回答:1

在大多数游戏引擎中,都有一个可以作为函数编写的更新事件,但是我不知道如何在javascript中创建类似的东西,所以我想知道是否有人知道如何创建这样的系统

javascript
1个回答
0
投票

JavaScript没有“滴答率”。任何会定期对元素进行更新的系统都需要按一定的间隔进行更新。

您可以有一个主事件循环,该循环要求对许多其他功能进行更新,并以此方式创建行为类似于游戏引擎的系统,例如:

var mainEventLoop = setInterval(function() { // This will begin running as soon as the page loads

   // Then, all of the functions you want to run at this interval can be included here:
     updatePlayerSprite();

 if (enemyActive === true){
     updateEnemySprites();
     }
 if (prevPlayerHealth > currentPlayerHealth){
     updatePlayerHealthBar(currentPlayerHealth);
     }

    // etc.

    },50); // This (arbitrary example) number is the time in ms between each update

RPG Maker MV已在JS上构建了整个引擎。由于大部分界面都是基于视觉的,因此对于事件和JS而言,它是相当可靠的,但是它也允许您执行自己的自定义JS。如果您想了解一个独立的JS应用程序如何处理整个游戏引擎,它通常会在Steam上以$ 20的价格出售。

© www.soinside.com 2019 - 2024. All rights reserved.