update_loop.html 5.9 KB

1234567891011121314151617
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><meta name="keywords" content="basegame, control, input, init, keyinput, loop, states, state"><title>Main Update Loop</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/advanced/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/update_loop.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Main Update Loop</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#usage">Usage</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>Extending your application from com.jme3.app.<a href="../../jme3/intermediate/simpleapplication.html">SimpleApplication</a> provides you with an update loop. This is where you implement your game logic (game mechanics).</p></div>
  4. <div class="paragraph"><p>Some usage examples: Here you remote-control NPCs (computer controlled characters), generate game events, and respond to user input.</p></div>
  5. <div class="paragraph"><p>To let you see the main update loop in context, understand that the SimpleApplication does the following:</p></div>
  6. <div class="ulist"><ul><li><p><strong>Initialization</strong> – Execute <code>simpleInitApp()</code> method once.</p></li><li><p><strong>Main Update Loop</strong></p><div class="olist loweralpha"><ol class="loweralpha" type="a"><li><p>Input listeners respond to mouse clicks and keyboard presses – <a href="../../jme3/advanced/input_handling.html">Input handling</a></p></li><li><p>Update game state:</p><div class="olist lowerroman"><ol class="lowerroman" type="i"><li><p>Update overall game state – Execute <a href="../../jme3/advanced/application_states.html">Application States</a></p></li><li><p>User code update – Execute <code>simpleUpdate()</code> method</p></li><li><p>Logical update of entities – Execute <a href="../../jme3/advanced/custom_controls.html">Custom Controls</a></p></li></ol></div></li><li><p>Render audio and video</p><div class="olist lowerroman"><ol class="lowerroman" type="i"><li><p><a href="../../jme3/advanced/application_states.html">Application States</a> rendering.</p></li><li><p>Scene rendering.</p></li><li><p>User code rendering – Execute <code>simpleRender()</code> method.</p></li></ol></div></li><li><p>Repeat loop.</p></li></ol></div></li><li><p><strong>Quit</strong> – If user requests <code>exit()</code>, execute <code>cleanup()</code> and <code>destroy()</code>.<br>
  7. The jME window closes and the loop ends.</p></li></ul></div></div></div>
  8. <div class="sect1"><h2 id="usage">Usage</h2><div class="sectionbody"><div class="paragraph"><p>In a trivial <a href="../../jme3/intermediate/simpleapplication.html">SimpleApplication</a> (such as a <a href="../../jme3/beginner.html">Hello World tutorial</a>), all code is either in the <code>simpleInitApp()</code> (initialization) or <code>simpleUpdate()</code> (behaviour) method – or in a helper method/class that is called from one of these two. This trivial approach will make your main class very long, hard to read, and hard to maintain. You don&#8217;t need to load the whole scene at once, and you don&#8217;t need to run all conditionals tests all the time.</p></div>
  9. <div class="paragraph"><p>It&#8217;s a best practice to modularize your game mechanics and spread out initialization and update loop code over several Java objects:</p></div>
  10. <div class="ulist"><ul><li><p>Move modular code blocks from the <code>simpleInitApp()</code> method into <a href="../../jme3/advanced/application_states.html">AppStates</a>. Attach AppStates to initialize custom subsets of “one dungeon, and detach it when the player exits this “dungeon.<br>
  11. Examples: Weather/sky audio and visuals, physics collision shapes, sub-rootnodes of individual dungeons including dungeon NPCs, etc.</p></li><li><p>Move modular code blocks from the <code>simpleUpdate()</code> method into the update loops of <a href="../../jme3/advanced/custom_controls.html">Custom Controls</a> to control individual entity behavior (NPCs), and into the update method of <a href="../../jme3/advanced/application_states.html">AppStates</a> to control world events.<br>
  12. Examples: Weather behaviour, light behaviour, physics behaviour, individual NPC behaviour, trap behaviour, etc.</p></li></ul></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  13. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  14. indexName: 'jmonkeyengine',
  15. inputSelector: '#doc-search',
  16. debug: false // Set debug to true if you want to inspect the dropdown
  17. });</script></body></html>