Day 1: Introduction & Setting Up Phaser with React Native
In this 10-day tutorial series, you’ll learn how to build a cross-platform mobile game using React Native and Phaser, combining the power of mobile app development with a full-featured 2D game engine. Each tutorial will build progressively, with fully working code at every step.
๐ง What Youโll Learn Today
- Introduction to Phaser and React Native
- How to set up a React Native project with Expo
- How to integrate Phaser using a WebView
- Test a simple Phaser “Hello World” scene inside your mobile app
๐ง Why Phaser + React Native?
Phaser is a popular JavaScript game engine for creating 2D games. React Native is the go-to framework for building cross-platform mobile apps. When combined using a WebView
, you can run full Phaser games inside a mobile app, giving you the best of both worlds.
โ Prerequisites
- Node.js installed (download here)
- Expo CLI:
npm install -g expo-cli
- Basic knowledge of React Native
- A mobile emulator or Expo Go app installed on your device
๐ฆ Step 1: Initialize Your React Native Project
Use Expo to create the project:
expo init react-native-phaser-game
Choose blank (JavaScript) when prompted.
Navigate into your project:
cd react-native-phaser-game
Install WebView:
expo install react-native-webview
๐ฎ Step 2: Add a Phaser HTML Template
Create a folder assets/phaser
inside your project:
mkdir -p assets/phaser
Create a file game.html
inside assets/phaser
:
<!-- assets/phaser/game.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Phaser Game</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script>
const config = {
type: Phaser.AUTO,
width: 320,
height: 480,
scene: {
preload,
create,
},
};
function preload() {
this.load.image('logo', 'https://labs.phaser.io/assets/sprites/phaser3-logo.png');
}
function create() {
this.add.image(160, 240, 'logo');
}
new Phaser.Game(config);
</script>
</body>
</html>
๐ฑ Step 3: Load Phaser Game via WebView in React Native
Open App.js
and replace its contents with:
import React from 'react';
import { WebView } from 'react-native-webview';
import { Platform } from 'react-native';
export default function App() {
const source = Platform.select({
ios: require('./assets/phaser/game.html'),
android: { uri: 'file:///android_asset/game.html' },
});
return (
<WebView
originWhitelist={['*']}
source={source}
javaScriptEnabled={true}
domStorageEnabled={true}
style={{ flex: 1 }}
/>
);
}
โ ๏ธ Note for Android
To make it work on Android:
- You need to manually copy
game.html
into the correctandroid/app/src/main/assets/
directory using a postinstall script or custom script during build. - You can also host
game.html
temporarily on a local server usingpython3 -m http.server
or use theuri
method to fetch it.
๐ Run the App
Start the Expo server:
expo start
Scan the QR code in Expo Go (iOS/Android) and you should see the Phaser logo rendered inside your app.
โ What Youโve Achieved
- Created a React Native + Phaser development environment
- Displayed your first Phaser game scene in a WebView
- Set the stage for building an interactive game experience
๐ Up Next
In Day 2, weโll create a basic game loop, handle user input (touch/tap), and render sprites dynamically. Weโll also refactor code into separate files for better organization.
Stay tuned as we build a fully working mobile arcade game over 10 days!