user-components-start-update-methods.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html class="writer-html5" lang="en" >
  3. <head>
  4. <meta charset="utf-8" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Implementing behaviors with the Phaser events &mdash; Phaser Editor 2D Help</title>
  7. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  9. <link rel="stylesheet" href="../_static/css/custom.css" type="text/css" />
  10. <link rel="shortcut icon" href="../_static/icon.png"/>
  11. <!--[if lt IE 9]>
  12. <script src="../_static/js/html5shiv.min.js"></script>
  13. <![endif]-->
  14. <script src="../_static/jquery.js?v=5d32c60e"></script>
  15. <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
  16. <script src="../_static/documentation_options.js?v=a1f35292"></script>
  17. <script src="../_static/doctools.js?v=888ff710"></script>
  18. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  19. <script src="../_static/js/theme.js"></script>
  20. <link rel="index" title="Index" href="../genindex.html" />
  21. <link rel="search" title="Search" href="../search.html" />
  22. <link rel="next" title="A base class for your components" href="user-components-super-class.html" />
  23. <link rel="prev" title="The User Components compiler" href="user-components-compiler.html" />
  24. </head>
  25. <body class="wy-body-for-nav">
  26. <div class="wy-grid-for-nav">
  27. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  28. <div class="wy-side-scroll">
  29. <div class="wy-side-nav-search" style="background: #343131" >
  30. <a href="../index.html" class="icon icon-home">
  31. Phaser Editor 2D
  32. <img src="../_static/logo.png" class="logo" alt="Logo"/>
  33. </a>
  34. <div class="version">
  35. v3.67.0
  36. </div>
  37. <div role="search">
  38. <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
  39. <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
  40. <input type="hidden" name="check_keywords" value="yes" />
  41. <input type="hidden" name="area" value="default" />
  42. </form>
  43. </div>
  44. </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
  45. <ul class="current">
  46. <li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Introduction</a></li>
  47. <li class="toctree-l1"><a class="reference internal" href="../first-steps/index.html">First steps</a></li>
  48. <li class="toctree-l1"><a class="reference internal" href="../workbench/index.html">Workbench</a></li>
  49. <li class="toctree-l1"><a class="reference internal" href="../asset-pack-editor/index.html">Asset Pack Editor</a></li>
  50. <li class="toctree-l1 current"><a class="reference internal" href="index.html">Scene Editor</a><ul class="current">
  51. <li class="toctree-l2"><a class="reference internal" href="create-new-scene-file.html">Create a new Scene file</a></li>
  52. <li class="toctree-l2"><a class="reference internal" href="add-object.html">Adding an object to the scene</a></li>
  53. <li class="toctree-l2"><a class="reference internal" href="inspector-view.html">Inspector view</a></li>
  54. <li class="toctree-l2"><a class="reference internal" href="game-objects.html">Game Object types</a></li>
  55. <li class="toctree-l2"><a class="reference internal" href="scene-properties.html">Scene properties</a></li>
  56. <li class="toctree-l2"><a class="reference internal" href="arcade-physics.html">Arcade Physics</a></li>
  57. <li class="toctree-l2"><a class="reference internal" href="sprite-animations.html">Sprite animations</a></li>
  58. <li class="toctree-l2"><a class="reference internal" href="spine-animations.html">Spine animations</a></li>
  59. <li class="toctree-l2"><a class="reference internal" href="shader-effects.html">Shader Effects</a></li>
  60. <li class="toctree-l2"><a class="reference internal" href="working-with-parent-objects.html">Working with parent objects</a></li>
  61. <li class="toctree-l2"><a class="reference internal" href="object-list.html">Object List</a></li>
  62. <li class="toctree-l2"><a class="reference internal" href="input.html">Input</a></li>
  63. <li class="toctree-l2"><a class="reference internal" href="prefabs.html">Prefabs</a></li>
  64. <li class="toctree-l2"><a class="reference internal" href="script-node.html">Script Nodes</a></li>
  65. <li class="toctree-l2 current"><a class="reference internal" href="user-components.html">User Components</a><ul class="current">
  66. <li class="toctree-l3"><a class="reference internal" href="user-components-create-file.html">Create a new User Components file</a></li>
  67. <li class="toctree-l3"><a class="reference internal" href="user-components-editor.html">User Components Editor</a></li>
  68. <li class="toctree-l3"><a class="reference internal" href="user-components-compiler.html">The User Components compiler</a></li>
  69. <li class="toctree-l3 current"><a class="current reference internal" href="#">Implementing behaviors with the Phaser events</a></li>
  70. <li class="toctree-l3"><a class="reference internal" href="user-components-super-class.html">A base class for your components</a></li>
  71. <li class="toctree-l3"><a class="reference internal" href="user-components-awake-event.html">The awake event</a></li>
  72. <li class="toctree-l3"><a class="reference internal" href="user-components-instancing.html">Adding User Components to a Game Object</a></li>
  73. <li class="toctree-l3"><a class="reference internal" href="user-components-in-prefab-object.html">Adding User Components to a Prefab</a></li>
  74. </ul>
  75. </li>
  76. <li class="toctree-l2"><a class="reference internal" href="manipulation-tools.html">Manipulation tools</a></li>
  77. <li class="toctree-l2"><a class="reference internal" href="layout-tools.html">Layout tools</a></li>
  78. <li class="toctree-l2"><a class="reference internal" href="scene-compiler.html">The scene compiler</a></li>
  79. <li class="toctree-l2"><a class="reference internal" href="misc.html">Miscellaneous</a></li>
  80. <li class="toctree-l2"><a class="reference internal" href="troubleshooting.html">Troubleshooting</a></li>
  81. </ul>
  82. </li>
  83. <li class="toctree-l1"><a class="reference internal" href="../code-editor/index.html">Code Editor</a></li>
  84. <li class="toctree-l1"><a class="reference internal" href="../animations-editor/index.html">Animations Editor</a></li>
  85. <li class="toctree-l1"><a class="reference internal" href="../atlas-editor/index.html">Atlas Editor</a></li>
  86. <li class="toctree-l1"><a class="reference internal" href="../misc/index.html">Miscellaneous</a></li>
  87. <li class="toctree-l1"><a class="reference internal" href="../pricing/index.html">License and pricing</a></li>
  88. </ul>
  89. </div>
  90. </div>
  91. </nav>
  92. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #343131" >
  93. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  94. <a href="../index.html">Phaser Editor 2D</a>
  95. </nav>
  96. <div class="wy-nav-content">
  97. <div class="rst-content">
  98. <div role="navigation" aria-label="Page navigation">
  99. <ul class="wy-breadcrumbs">
  100. <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
  101. <li class="breadcrumb-item"><a href="index.html">Scene Editor</a></li>
  102. <li class="breadcrumb-item"><a href="user-components.html">User Components</a></li>
  103. <li class="breadcrumb-item active">Implementing behaviors with the Phaser events</li>
  104. <li class="wy-breadcrumbs-aside">
  105. </li>
  106. </ul>
  107. <hr/>
  108. </div>
  109. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  110. <div itemprop="articleBody">
  111. <section id="implementing-behaviors-with-the-phaser-events">
  112. <h1>Implementing behaviors with the Phaser events<a class="headerlink" href="#implementing-behaviors-with-the-phaser-events" title="Link to this heading"></a></h1>
  113. <p>Many <a class="reference external" href="../scene-editor/user-components.html">user components</a> you will create will implement certain behavior of an object. This behavior should be initialized and later updated at every step of the game loop.</p>
  114. <p>Game engines like <a class="reference external" href="https://unity.com">Unity</a>, that support a similar design pattern, <a class="reference external" href="https://docs.unity3d.com/Manual/CreatingAndUsingScripts.html">use a Start and Update methods</a> to implement the behavior of the component.</p>
  115. <p>The <a class="reference external" href="https://phaser.io">Phaser</a> framework does not provide a similar approach, components are not part of the <a class="reference external" href="https://phaser.io">Phaser</a> API. However, <a class="reference external" href="https://phaser.io">Phaser</a> provides a lot of events that you can handle and implement the gameplay. For example, you can simulate the Unity scripting using the <a class="reference external" href="../scene-editor/user-components.html">user components</a> and the <a class="reference external" href="https://phaser.io">Phaser</a> events. You can create an <strong>EventComponent</strong> class that handles the events:</p>
  116. <div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kd">class</span><span class="w"> </span><span class="nx">EventComponent</span><span class="w"> </span><span class="p">{</span>
  117. <span class="w"> </span><span class="cm">/**</span>
  118. <span class="cm"> * @param {Phaser.GameObjects.GameObject} gameObject</span>
  119. <span class="cm"> */</span>
  120. <span class="w"> </span><span class="kr">constructor</span><span class="p">(</span><span class="nx">gameObject</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  121. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">scene</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">gameObject</span><span class="p">.</span><span class="nx">scene</span><span class="p">;</span>
  122. <span class="w"> </span><span class="c1">// first time the scene is updated, call the `start` method</span>
  123. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">scene</span><span class="p">.</span><span class="nx">events</span><span class="p">.</span><span class="nx">once</span><span class="p">(</span><span class="nx">Phaser</span><span class="p">.</span><span class="nx">Scenes</span><span class="p">.</span><span class="nx">Events</span><span class="p">.</span><span class="nx">UPDATE</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">start</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">);</span>
  124. <span class="w"> </span><span class="c1">// each time the scene is updated, call the `update` method</span>
  125. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">scene</span><span class="p">.</span><span class="nx">events</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="nx">Phaser</span><span class="p">.</span><span class="nx">Scenes</span><span class="p">.</span><span class="nx">Events</span><span class="p">.</span><span class="nx">UPDATE</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">update</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">);</span>
  126. <span class="w"> </span><span class="c1">// if the object is destroyed, call the `destroy` method</span>
  127. <span class="w"> </span><span class="nx">gameObject</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="nx">Phaser</span><span class="p">.</span><span class="nx">GameObjects</span><span class="p">.</span><span class="nx">Events</span><span class="p">.</span><span class="nx">DESTROY</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">destroy</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">);</span>
  128. <span class="w"> </span><span class="p">}</span>
  129. <span class="w"> </span><span class="nx">start</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
  130. <span class="w"> </span><span class="c1">// to be overridden in derived classes</span>
  131. <span class="w"> </span><span class="p">}</span>
  132. <span class="w"> </span><span class="nx">update</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
  133. <span class="w"> </span><span class="c1">// to be overridden in derived classes</span>
  134. <span class="w"> </span><span class="p">}</span>
  135. <span class="w"> </span><span class="nx">destroy</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
  136. <span class="w"> </span><span class="c1">// the object is destroyed, so we remove all the event handlers</span>
  137. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">scene</span><span class="p">.</span><span class="nx">events</span><span class="p">.</span><span class="nx">off</span><span class="p">(</span><span class="nx">Phaser</span><span class="p">.</span><span class="nx">Scenes</span><span class="p">.</span><span class="nx">Events</span><span class="p">.</span><span class="nx">UPDATE</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">start</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">);</span>
  138. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">scene</span><span class="p">.</span><span class="nx">events</span><span class="p">.</span><span class="nx">off</span><span class="p">(</span><span class="nx">Phaser</span><span class="p">.</span><span class="nx">Scenes</span><span class="p">.</span><span class="nx">Events</span><span class="p">.</span><span class="nx">UPDATE</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">update</span><span class="p">,</span><span class="w"> </span><span class="k">this</span><span class="p">);</span>
  139. <span class="w"> </span><span class="p">}</span>
  140. <span class="p">}</span>
  141. </pre></div>
  142. </div>
  143. <p>Then, you can set the <strong>EventComponent</strong> class as super class to all your components:</p>
  144. <img alt="Setting a super class to components." src="../_images/scene-editor-user-components-super-class-07302020.webp" />
  145. <p>Now, you can override the <strong>start</strong>, <strong>update</strong>, and <strong>destroy</strong> methods in the component classes.</p>
  146. <div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kd">class</span><span class="w"> </span><span class="nx">HorizontalMove</span><span class="w"> </span><span class="k">extends</span><span class="w"> </span><span class="nx">EventComponent</span><span class="w"> </span><span class="p">{</span>
  147. <span class="w"> </span><span class="p">...</span>
  148. <span class="w"> </span><span class="nx">update</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
  149. <span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">gameObject</span><span class="p">.</span><span class="nx">x</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">deltaX</span><span class="p">;</span>
  150. <span class="w"> </span><span class="p">}</span>
  151. <span class="p">}</span>
  152. </pre></div>
  153. </div>
  154. <p>The <code class="docutils literal notranslate"><span class="pre">EventComponent</span></code> class is just an example of what you can do. It has a weak point: not all components need to listen to all the events. It is a waste of resources adding scene listeners in all the cases.</p>
  155. <p>Since v3.13.0, the editor can “write” for you a base class that could be a better solution. However, you can change its code and adapt it to your specific domain. Check the next section for more details.</p>
  156. </section>
  157. </div>
  158. </div>
  159. <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
  160. <a href="user-components-compiler.html" class="btn btn-neutral float-left" title="The User Components compiler" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
  161. <a href="user-components-super-class.html" class="btn btn-neutral float-right" title="A base class for your components" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
  162. </div>
  163. <hr/>
  164. <div role="contentinfo">
  165. <p>&#169; Copyright 2016-2024, Arian Fornaris.</p>
  166. </div>
  167. Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
  168. <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
  169. provided by <a href="https://readthedocs.org">Read the Docs</a>.
  170. </footer>
  171. </div>
  172. </div>
  173. </section>
  174. </div>
  175. <script>
  176. jQuery(function () {
  177. SphinxRtdTheme.Navigation.enable(true);
  178. });
  179. </script>
  180. <!-- Theme Analytics -->
  181. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-67206336-2"></script>
  182. <script>
  183. window.dataLayer = window.dataLayer || [];
  184. function gtag(){dataLayer.push(arguments);}
  185. gtag('js', new Date());
  186. gtag('config', 'UA-67206336-2', {
  187. 'anonymize_ip': false,
  188. });
  189. </script>
  190. </body>
  191. </html>