avatarIvan Franchin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4023

Abstract

())</pre></div><p id="447a">The provided JavaScript code defines a <code>Box2DCreateJS</code> namespace object and encapsulates its code within an immediately-invoked function expression (IIFE) for encapsulation and scoping.</p><p id="69c4">It includes a <code>MyGame</code> constructor function that is assigned to <code>Box2DCreateJS.MyGame</code>. The <code>initialize</code> method is added to the <code>MyGame</code> prototype, which is expected to be called after creating an instance of <code>MyGame</code>.</p><p id="7597">Finally, two constants, <code>easeljsCanvas</code> and <code>box2dCanvas</code> are set up by fetching the corresponding DOM elements with the IDs <code>easeljsCanvas</code> and <code>box2dCanvas</code>.</p><h1 id="220e">Create index.html</h1><p id="de01">In <code>my-game</code> folder, create an HTML file called <code>index.html</code>. Copy and paste the content below:</p><div id="65e6"><pre><span class="hljs-meta"><!DOCTYPE <span class="hljs-keyword">html</span>></span> <span class="hljs-tag"><<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>></span>

<span class="hljs-tag"><<span class="hljs-name">head</span>></span> <span class="hljs-tag"><<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>></span> <span class="hljs-tag"><<span class="hljs-name">title</span>></span>My Game<span class="hljs-tag"></<span class="hljs-name">title</span>></span>

<span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">'stylesheet'</span> <span class="hljs-attr">href</span>=<span class="hljs-string">'box2dcreatejs/css/cssreset-min.css'</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text/css'</span> <span class="hljs-attr">media</span>=<span class="hljs-string">'all'</span>/&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="language-css">
    <span class="hljs-selector-id">#easeljsCanvas</span>, <span class="hljs-selector-id">#box2dCanvas</span> {
        <span class="hljs-attribute">position</span>: absolute;
    }

    <span class="hljs-selector-id">#easeljsCanvas</span> {
      <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#8DB8E3</span>;
    }
</span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"MyGame.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript">
    <span class="hljs-variable language_">window</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">"load"</span>, <span class="hljs-function">() =&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Box2DCreateJS</span>.<span class="hljs-title class_">MyGame</span>())
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag"></<span class="hljs-name">head</span>></span>

<span class="hljs-tag"><<span class="hljs-name">body</span>></span> <span class="hljs-tag"><<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"easeljsCanvas"</span> <span class="hljs-attr">width</span>=<span class="hljs-string">"980"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"500"</span>></span><span class="hljs-tag"></<span class="hljs-name">canvas</span>></span> <span class="hljs-tag"><<span class="hljs-name">canvas</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"box2dCanvas"</span> <s

Options

pan class="hljs-attr">width</span>=<span class="hljs-string">"980"</span> <span class="hljs-attr">height</span>=<span class="hljs-string">"500"</span>></span><span class="hljs-tag"></<span class="hljs-name">canvas</span>></span> <span class="hljs-tag"></<span class="hljs-name">body</span>></span>

<span class="hljs-tag"></<span class="hljs-name">html</span>></span></pre></div><p id="1e64">The provided HTML code represents a basic HTML document for a web-based game.</p><p id="cf6f">It includes references to CSS and JavaScript files, defines some styles for canvas elements, and initializes the application by creating a new instance of the <code>MyGame</code> constructor function from the <code>MyGame.js</code> file when the HTML document finishes loading.</p><p id="300a">The <code><canvas></code> elements with IDs <code>easeljsCanvas</code> and <code>box2dCanvas</code> will be used for rendering EaselJS and Box2D graphics, respectively. Both canvas have a width of <b>980</b> <b>pixels</b> and a height of <b>500</b> <b>pixels</b>.</p><figure id="74ef"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ps_MVOFTpx93-WtKTD2Sgg.png"><figcaption></figcaption></figure><h1 id="96a0">Create the images folder</h1><p id="1923">Inside the <code>my-game</code> folder, let’s create another folder called <code>images</code>. In it, we will save all the images used in our games.</p><h1 id="97d8">Project Structure of Folders and Files</h1><p id="62ba">The final project must have the following configuration of folders and files</p><div id="c0c8"><pre>. ├── box2dcreatejs │ ├── css │ └── js ├── images ├── index.html └── MyGame.js</pre></div><p id="a853">That is all! You are ready to start creating 2D Physics Games in JavaScript using <b>Box2DCreateJS</b>.</p><h1 id="65b7">Additional Readings</h1><p id="fb84">The source code can be found at <a href="https://github.com/ivangfr/box2dcreatejs"><b>Box2DCreateJS</b> <b>GitHub</b> <b>Repository</b></a>.</p><div id="c8f7" class="link-block"> <a href="https://readmedium.com/box2dcreatejs-creating-a-ball-and-interacting-with-it-using-the-cursor-a7072bbe2d7e"> <div> <div> <h2>Box2DCreateJS: Creating a ball and interacting with it using the cursor</h2> <div><h3>Interactive ball creation and cursor interaction tutorial</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*VCwe6dMRfi3sCJvd)"></div> </div> </div> </a> </div><div id="2824" class="link-block"> <a href="https://readmedium.com/box2dcreatejs-creating-a-monster-truck-game-225193431735"> <div> <div> <h2>Box2DCreateJS: Creating a monster truck game</h2> <div><h3>Building an exciting monster truck game from scratch</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*tSKOY21nQ77k4vZM)"></div> </div> </div> </a> </div><h1 id="878d">Support and Engagement</h1><p id="c13e">If you enjoyed this article and would like to show your support, please consider taking the following actions:</p><ul><li>👏 Engage by clapping, highlighting, and replying to my story. I’ll be happy to answer any of your questions;</li><li>🌐 Share my story on Social Media;</li><li>🔔 Follow me on: <a href="https://medium.com/@ivangfr">Medium</a> | <a href="https://www.linkedin.com/in/ivanfranchin/">LinkedIn</a> | <a href="https://twitter.com/ivangfr">Twitter</a> | <a href="https://github.com/ivangfr">GitHub</a>;</li><li>✉️ <a href="https://medium.com/@ivangfr/subscribe">Subscribe to my newsletter</a>, so you don’t miss out on my latest posts.</li></ul></article></body>

Box2DCreateJS = Box2d + CreateJS

Box2DCreateJS: Creating the project initial setup

A guide to setting up the initial project configuration

Photo by Elena Joland on Unsplash

In this article, I will explain how to create the project initial setup for Box2DCreateJS. It can be used as a template and starting point for the development of 2D physics games using JavaScript.

If you don’t know what Box2DCreateJS is, take a look at the article linked below:

Let’s get started!

Download and Unzip Box2DCreateJS

Access https://ivangfr.github.io/box2dcreatejs/. At the bottom of the page, you will find the Downloads section. Go ahead and download the latest version.

Unzip the file in a folder where you will place all the game files. I will use a folder called my-game.

Once extracted, the folder name will be something like box2dcreatejs-x.y.z, where x.y.z is the version you have downloaded. Let’s remove the -x.y.z so that the final folder name should be box2dcreatejs.

Create MyGame.js

In my-game folder, create a JavaScript file called MyGame.js. Copy and paste the following content:

this.Box2DCreateJS = this.Box2DCreateJS || {};

(function () {

    function MyGame() {
        this.initialize()
    }

    Box2DCreateJS.MyGame = MyGame

    MyGame.prototype.initialize = function () {
        const easeljsCanvas = document.getElementById("easeljsCanvas")
        const box2dCanvas = document.getElementById("box2dCanvas")
    }

}())

The provided JavaScript code defines a Box2DCreateJS namespace object and encapsulates its code within an immediately-invoked function expression (IIFE) for encapsulation and scoping.

It includes a MyGame constructor function that is assigned to Box2DCreateJS.MyGame. The initialize method is added to the MyGame prototype, which is expected to be called after creating an instance of MyGame.

Finally, two constants, easeljsCanvas and box2dCanvas are set up by fetching the corresponding DOM elements with the IDs easeljsCanvas and box2dCanvas.

Create index.html

In my-game folder, create an HTML file called index.html. Copy and paste the content below:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>My Game</title>

    <link rel='stylesheet' href='box2dcreatejs/css/cssreset-min.css' type='text/css' media='all'/>
    <style>
        #easeljsCanvas, #box2dCanvas {
            position: absolute;
        }

        #easeljsCanvas {
          background-color: #8DB8E3;
        }
    </style>

    <script src="MyGame.js"></script>
    <script>
        window.addEventListener("load", () => new Box2DCreateJS.MyGame())
    </script>
</head>

<body>
    <canvas id="easeljsCanvas" width="980" height="500"></canvas>
    <canvas id="box2dCanvas" width="980" height="500"></canvas>
</body>

</html>

The provided HTML code represents a basic HTML document for a web-based game.

It includes references to CSS and JavaScript files, defines some styles for canvas elements, and initializes the application by creating a new instance of the MyGame constructor function from the MyGame.js file when the HTML document finishes loading.

The <canvas> elements with IDs easeljsCanvas and box2dCanvas will be used for rendering EaselJS and Box2D graphics, respectively. Both canvas have a width of 980 pixels and a height of 500 pixels.

Create the images folder

Inside the my-game folder, let’s create another folder called images. In it, we will save all the images used in our games.

Project Structure of Folders and Files

The final project must have the following configuration of folders and files

.
├── box2dcreatejs
│   ├── css
│   └── js
├── images
├── index.html
└── MyGame.js

That is all! You are ready to start creating 2D Physics Games in JavaScript using Box2DCreateJS.

Additional Readings

The source code can be found at Box2DCreateJS GitHub Repository.

Support and Engagement

If you enjoyed this article and would like to show your support, please consider taking the following actions:

  • 👏 Engage by clapping, highlighting, and replying to my story. I’ll be happy to answer any of your questions;
  • 🌐 Share my story on Social Media;
  • 🔔 Follow me on: Medium | LinkedIn | Twitter | GitHub;
  • ✉️ Subscribe to my newsletter, so you don’t miss out on my latest posts.
Game Development
Box2d
Createjs
JavaScript
Technology
Recommended from ReadMedium