UNPKG

jsge

Version:

Javascript Game Engine

114 lines (99 loc) 6.1 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="expires" content="Wen, 05 Jun 2024 07:01:00 GMT"> <title>JSDoc: Tutorial: How to Create Animations</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="stage-title">Tutorial: How to Create Animations</h1> <section> <header> <h2>How to Create Animations</h2> </header> <article> <p>How to Create Animations with Sprite Indexes:</p> <ol> <li> <p>Take an image with animations.<br> Here is an example fireball 128x16 px image, each animation step is 16x16 px:<br> <img src="fireball128x16.png" alt="fireball"></p> </li> <li> <p><a href="tutorial-assets_manager.html">Add and load it to the project</a>:</p> <pre class="prettyprint source"><code>this.iLoader.addImage(&quot;image_key&quot;, &quot;./fireball128x16.png&quot;); </code></pre> </li> <li> <p>Create an 16x16 DrawImageObject with the same key:</p> <pre class="prettyprint source"><code>const posX = 100; const posY = 200; const imageW = 16; const imageH = 16; this.fireball = this.draw.image(posX, posY, imageW, imageH, &quot;image_key&quot;); </code></pre> </li> <li> <p>Add an animation event:</p> <pre class="prettyprint source"><code>this.fireball.addAnimation(&quot;startAnimation&quot;, [0,1,2,3], isLoop = false); </code></pre> </li> <li> <p>Start the animation by emitting the event:</p> <pre class="prettyprint source"><code>this.fireball.emit(&quot;startAnimation&quot;); </code></pre> <p>This will run through the image indexes 0,1,2,3.</p> </li> <li> <p>To loop the animation indexes, add a third parameter as <code>true</code>:</p> <pre class="prettyprint source"><code>this.fireball.addAnimation(&quot;startAnimation&quot;, [0,1,2,3], true); </code></pre> <p>The animation will continue looping until <code>stopRepeatedAnimation()</code> is called or the object is destroyed:</p> <pre class="prettyprint source"><code>this.fireball.stopRepeatedAnimation(&quot;startAnimation&quot;); </code></pre> </li> <li> <p>The fourth parameter determines how many cycles each frame is shown.<br> The default value is 1. If you want to make the animation slower, increase this value:</p> <pre class="prettyprint source"><code>this.fireball.addAnimation(&quot;startAnimation&quot;, [0,1,2,3], true, 5); </code></pre> </li> <li> <p>In jsge@1.4.0, frame time support was added. Instead of an array of IDs,<br> you can now add an array of objects:</p> <pre class="prettyprint source"><code>this.fireball.addAnimation(&quot;startAnimation&quot;, [{duration: 150, id:0},{duration:100, id:1},{ duration:100, id:2},{duration:100, id:3}], true); </code></pre> <p>Here, <code>duration</code> is the frame time in milliseconds.</p> </li> </ol> <p><a href="tutorial-how_to_load_and_use_tilemaps.html">Tiled animations</a> support.</p> <h2>Live example</h2> <p class="codepen" data-height="500" data-default-tab="js,result" data-slug-hash="zYeQoGY" data-user="yaalfred" style="height: 500px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;"> <span>See the Pen <a href="https://codepen.io/yaalfred/pen/zYeQoGY"> Untitled</a> by Arturas-Alfredas Lapinskas (<a href="https://codepen.io/yaalfred">@yaalfred</a>) on <a href="https://codepen.io">CodePen</a>.</span> </p> <script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script> </article> </section> </div> <nav> <div class="switcher"><ul><li class="active"><a href="/">1.5.9</a></li></div> <h2><a href="/">Home</a></h2><h3>Classes</h3><ul><li><a href="DrawCircleObject.html">DrawCircleObject</a></li><li><a href="DrawConusObject.html">DrawConusObject</a></li><li><a href="DrawImageObject.html">DrawImageObject</a></li><li><a href="DrawLineObject.html">DrawLineObject</a></li><li><a href="DrawObjectFactory.html">DrawObjectFactory</a></li><li><a href="DrawPolygonObject.html">DrawPolygonObject</a></li><li><a href="DrawRectObject.html">DrawRectObject</a></li><li><a href="DrawShapeObject.html">DrawShapeObject</a></li><li><a href="DrawTextObject.html">DrawTextObject</a></li><li><a href="DrawTiledLayer.html">DrawTiledLayer</a></li><li><a href="GameStage.html">GameStage</a></li><li><a href="GameStageData.html">GameStageData</a></li><li><a href="IExtension.html">IExtension</a></li><li><a href="INetwork.html">INetwork</a></li><li><a href="IRender.html">IRender</a></li><li><a href="ISystem.html">ISystem</a></li><li><a href="ISystemAudio.html">ISystemAudio</a></li><li><a href="RenderLoop.html">RenderLoop</a></li><li><a href="RenderLoopDebug.html">RenderLoopDebug</a></li><li><a href="System.html">System</a></li><li><a href="SystemSettings.html">SystemSettings</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-application_scheme.html">Application Scheme</a></li><li><a href="tutorial-assets_manager.html">Assets Manager</a></li><li><a href="tutorial-common_issues.html">Common Issues</a></li><li><a href="tutorial-how_to_add_and_use_audio.html">How to add and use audio</a></li><li><a href="tutorial-how_to_do_animations.html">How to Create Animations</a></li><li><a href="tutorial-how_to_load_and_use_tilemaps.html">How to Load and Use Tilemaps</a></li><li><a href="tutorial-quick_start.html">Quick Start</a></li><li><a href="tutorial-spine_animations.html">How to Add Spine Animations</a></li><li><a href="tutorial-stages_lifecycle.html">Stages Lifecycle</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Wed Jun 18 2025 06:53:54 GMT+0000 (Coordinated Universal Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>