Git Product home page Git Product logo

nativescript-phaser-ce's Introduction

nativescript-phaser-ce

The source is now managed at https://github.com/NativeScript/canvas

Tools for using Phaser-ce to build native 2D games in NativeScript ๐Ÿ‘พ

Installation

npm i nativescript-phaser-ce

Usage

Import the library into your JavaScript file:

import TNSPhaser from "nativescript-phaser-ce";

Functions

TNSPhaser.game({ canvas, renderer: Phaser.WEBGL || Phaser.CANVAS, ...extras })

Given a canvas from a TNSCanvas, return a Phaser.Game that draws into it.

Props

Property Type Description Default Value
canvas TNSCanvas Required: canvas that the Phaser.Game will render to null
renderer number? Optional: choose the renderer type e.g Phaser.CANVAS (1) , Phaser.WEBGL(2) 1
width number? Optional: height of the Phaser.Game canvas height
height number? Optional: width of the Phaser.Game canvas width
title string? Optional: title of the Phaser.Game "tns-phaser-game"
preventLoop boolean? Optional: Prevents the app from calling canvas.nativeView.flush() every frame false

Returns

Property Type Description
game Phaser.Game The Phaser-ce game used for rendering game logic

Example

const game = TNSPhaser.game({ canvas });

What does it do?

Under the hood, TNSPhaser is maintaining global instances of a few libraries.

window.PIXI = require("phaser-ce/build/custom/pixi");
window.p2 = require("phaser-ce/build/custom/p2");
window.Phaser = require("phaser-ce/build/phaser");

Other libs can be included but are not required. For instance you can import the custom Creature lib the same way. We also override the PIXI.WebGLRenderer.updateTexture to make it compatible with NativeScript.

Finally when a new instance of TNSPhaser.Game is created, we set the document.readyState to 'complete' and save the global instance of context

global.__context = context;
global.document.readyState = "complete";

Then we create a standard render loop and call canvas.nativeView.flush() to flush the frame queue and render our context.

const render = () => {
  requestAnimationFrame(render);
  canvas.nativeView.flush();
};

License

Apache License Version 2.0, January 2004

nativescript-phaser-ce's People

Contributors

triniwiz avatar

Stargazers

 avatar

Watchers

 avatar  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.