Assume you want to render an image
Prepare:
- jsge is a web application. To run it, you will need a webserver.
- In this tutorial we will use nodejs webserver. So first you will need to install nodejs and npm.
- Then create a folder which will contain game files, put there an image with name "images.jpg".
- Create a package.json file which will store information about the game and its main dependencies. It can be done with from command line(terminal):
npm init
- After that install jsge:
npm i jsge
- install a webserver:
npm i http-server
- Put a command to run that server in the package.json:
"scripts": {
"start": "http-server -c-1 -p 9000 -o /"
},
- Now you can start the server from command line(terminal):
npm start
- Check http://127.0.0.1:9000 in the browser, it will show your folder structure.
App logic:
- By default server runs index.html file, so lets create it:
<!DOCTYPE html>
<head>
<script type="module" src="./index.js"></script>
</head>
<body>
<div id="game_map"></div>
</body>
- Then create index.js file, which will store app logic.
- Inside index.js create a System instance, passing game options, or a SystemSettings object and game canvas container:
import { System, SystemSettings } from "/node_modules/jsge/src/index.js";
const app = new System(SystemSettings, document.getElementById("game_map"));
- Create you game pages using classes extended from GameStage:
import { ..., GameStage } from "/node_modules/jsge/src/index.js";
class CustomPage extends GameStage {
}
- Add image passing image key and path to CustomPage.iLoader.addImage() in the stage register() stage:
class CustomPage extends GameStage {
register() {
this.iLoader.addImage("image_key", "/images.jpg");
}
}
- Create an DrawImageObject and add it to the stage, use image key, added on step 5:
...
this.player = this.draw.image(100, 200, 16, 28, "image_key", 0);
}
}
- Register pages in the application:
app.registerStage("CustomPageKey", CustomPage);
- Run preloadAllData() to load all data you added on step 5:
app.preloadAllData().then(() => {
- After preloadAllData() resolves, start the stage rendering with app.iSystem.startGameStage(pageKey):
app.preloadAllData().then(() => {
app.iSystem.startGameStage("CustomPageKey");
});
- Now visit http://127.0.0.1:9000
- Your image now will be rendered!
Use document.addEventListener to attach mouse or keyboard controllers and
move attached object on the screen changing x, y, or rotation properties
Live example:
See the Pen JsGE basic example by Arturas-Alfredas Lapinskas (@yaalfred) on CodePen.