GithubHelp home page GithubHelp logo

ricardolopezrey / phaser-on-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from geckosio/phaser-on-nodejs

0.0 0.0 0.0 225 KB

Allows you to run Phaser 3 game (including Phaser's physics engines) on Node.js

License: MIT License

TypeScript 22.77% JavaScript 77.23%

phaser-on-nodejs's Introduction

logo

Phaser on Node.js

Allows you to run Phaser 3 games (including Phaser's physics engines) on Node.js.

Github Workflow David npm NPM Codecov

Install

npm install @geckos.io/phaser-on-nodejs

How to use

require('@geckos.io/phaser-on-nodejs')
// or with es6
import '@geckos.io/phaser-on-nodejs'

Features

  • Phaser Physics (Arcade and Matter)
  • Load Images and SpriteSheets
  • Load TileMaps
  • Adjustable Frame Rate
  • Allows to use Multiple Scenes

Examples

Basic Setup

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)

Loading Assets

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)
  }
}

Compatible Phaser versions

For now, it has not been tested with Phaser 2, but it works well with Phaser 3.

phaser-on-nodejs's People

Contributors

yandeu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.