expo-phaser
Tools for using Phaser-ce to build native 2D games in Expo 👾
Installation
yarn add expo-phaser
Usage
Import the library into your JavaScript file:
;
Functions
ExpoPhaser.game({ context: WebGLRenderingContext, ...extras })
Given a context
from an
Expo.GLView
, return a
Phaser.Game
that draws into it.
Props
Property | Type | Description | Default Value |
---|---|---|---|
context | WebGLRenderingContext | Required: context that the Phaser.Game will render to |
null |
width | number? | Optional: height of the Phaser.Game |
context.drawingBufferWidth |
height | number? | Optional: width of the Phaser.Game |
context.drawingBufferHeight |
title | string? | Optional: title of the Phaser.Game |
"expo-phaser-game" |
preventLoop | boolean? | Optional: Prevents the app from calling context.endFrameEXP() every frame |
false |
Returns
Property | Type | Description |
---|---|---|
game | Phaser.Game |
The Phaser-ce game used for rendering game logic |
Example
const game = ExpoPhaser;
What does it do?
Under the hood, ExpoPhaser is maintaining global instances of a few libraries.
windowPIXI = ;windowp2 = ;windowPhaser = ;
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 Expo.
Finally when a new instance of Expo.Game
is created, we set the document.readyState
to 'complete'
and save the global instance of context
global__context = context;globaldocumentreadyState = 'complete';
Then we create a standard render loop and call context.endFrameEXP()
to flush the frame queue and render our context through EXGL
.
const render = { ; context;};
Example
It's important to note that you must preload all of your assets before starting the app, as the Phaser.State.preload
method cannot be asynchronous.
Creating a game in Expo is very simple with ExpoPhaser
, we preload our assets, create a view, initialize our game, then add our assets.
We create an Expo.GLView
to render our game to.
return <Expo.GLView = = />;
Then we create our Phaser.Game
instance and assign it a playable state. We can then choose to start said state.
{ const game = ExpoPhaser; gamestate; gamestatestart'Playable';}
Preloading
In React Native all assets must be static resources, because of this we must create a reference to all the assets we may use, then download them and get their local URI.
Expo has a convenient way of saving reference. We preload an Expo.Asset
then if we create the same instance later we can simple call asset.localUri
.
In a standard Phaser app we would load an asset like this:
gameload;
In expo we would load it like this:
const preloadedExpoAsset = ExpoAssetawait preloadedExpoAsset; ... gameload;
All together
This example shows how to load an animated texture atlas and apply arcade physics to it.
;;; const Assets = 'man.png': 'man.json': ; Component state = loading: true ; async { const downloads = ; for let key of Object const asset = ExpoAsset; downloads; await Promisealldownloads; this; } { if thisstateloading return <ExpoAppLoading />; return <ExpoGLView style= flex: 1 onContextCreate= /> ; } { const game = ExpoPhaser; gamestate; gamestatestart'Playable';}