Assume you want to render an image
Prepare:
- jsge is nodejs application, 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
- jsge is designed to work from browser so you will need a server to run it locally:
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 name:
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 ScreenPage:
import { ..., ScreenPage } from "/node_modules/jsge/src/index.js";
class CustomPage extends ScreenPage {
}
- Add image passing image key and path to CustomPage.loader.addImage() in the page register() stage:
class CustomPage extends ScreenPage {
register() {
this.loader.addImage("image_key", "/images.jpg");
}
}
- Create CanvasView on CustomPage.init(), or CustomPage.start() stages:
class CustomPage extends ScreenPage {
...
init() {
this.createCanvasView("view_key");
...
- Attach data you added on step 5.to the view from step 6.:
...
this.player = this.draw.image(100, 200, 16, 28, "image_key", 0);
this.addRenderObject("view_key", this.player);
}
}
- Register pages in the application:
app.registerPage("CustomPageKey", CustomPage);
- Run preloadAllData() to load all data you added on step 5:
app.preloadAllData().then(() => {
- After preloadAllData() resolves, start the page rendering with app.system.startScreenPage(pageKey):
app.preloadAllData().then(() => {
app.system.startScreenPage("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