npm install @geckos.io/phaser-on-nodejs
require('@geckos.io/phaser-on-nodejs')
// or with es6
import '@geckos.io/phaser-on-nodejs'
- Phaser Physics (Arcade and Matter)
- Load Images and SpriteSheets
- Load TileMaps
- Adjustable Frame Rate
- Allows to use Multiple Scenes
Install and require phaser
and @geckos.io/phaser-on-nodejs
. Make sure you use Phaser in headless mode on the server { type: Phaser.HEADLESS }
require('@geckos.io/phaser-on-nodejs')
const Phaser = require('phaser')
// set the fps you need
const FPS = 30
global.phaserOnNodeFPS = FPS // default is 60
// your MainScene
class MainScene extends Phaser.Scene {
constructor() {
super('MainScene')
}
create() {
console.log('it works!')
}
}
// prepare the config for Phaser
const config = {
type: Phaser.HEADLESS,
width: 1280,
height: 720,
banner: false,
audio: false,
scene: [MainScene],
fps: {
target: FPS,
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
},
},
}
// start the game
new Phaser.Game(config)
You can load textures (images, spritesheets etc.) on the server.
preload() {
// use a relative path
this.load.image('star', '../assets/star.png')
}
create() {
const star = this.physics.add.sprite(400, 300, 'star')
}
But to save some memory, I recommend the following approach instead:
class Star extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
// pass empty string for the texture
super(scene, x, y, '')
scene.add.existing(this)
scene.physics.add.existing(this)
// set the width and height of the sprite as the body size
this.body.setSize(24, 22)
}
}
For now, it has not been tested with Phaser 2, but it works well with Phaser 3.