Skip to content

Commit 745da81

Browse files
committed
Phaser Tutorial: upgrade to 0.16
1 parent 3b85568 commit 745da81

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

pages/tutorial/phaser/basic-player-movement.mdx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ title: "1: Basic Player Movement"
44
import { Callout, Cards, Tabs } from "nextra/components";
55
import { DownloadIcon, LinkIcon } from '@primer/octicons-react'
66

7-
<Callout type="warning">
8-
This tutorial still uses version 0.15. We're gradually migrating content to version 0.16.
9-
</Callout>
10-
117
# 1: Basic Player Movement
128

139
This guide will show you how you can build a multiplayer experience with Colyseus Multiplayer Framework and Phaser.
@@ -333,6 +329,14 @@ For this demo, we only need one sprite to represent the player, which we are goi
333329

334330
After a connection with the room has been established, the client-side can start listening for state changes, and create a visual representation of the data in the server.
335331

332+
In order to attach callbacks to the state, we need to import the `getStateCallbacks()` function from `colyseus.js`:
333+
334+
```typescript
335+
import { getStateCallbacks } from "colyseus.js";
336+
// (...)
337+
const $ = getStateCallbacks(this.room);
338+
```
339+
336340
### Adding new players
337341

338342
As per [Room State and Schema](#room-state-and-schema) section, whenever the server accepts a new connection - the `onJoin()` method is creating a new Player instance within the state.
@@ -341,7 +345,7 @@ We're going to listen to this event on the client-side now:
341345

342346
```ts
343347
// (...)
344-
this.room.state.players.onAdd((player, sessionId) => {
348+
$(this.room.state).players.onAdd((player, sessionId) => {
345349
//
346350
// A player has joined!
347351
//
@@ -355,6 +359,8 @@ When playing the Scene, you should see a message in the console of the browser w
355359
For the visual representation, we need to add a Phaser GameObject for each joining player, and keep a local reference to the GameObject based on their `sessionId`, so we can operate on them later:
356360

357361
```ts
362+
import { getStateCallbacks } from "colyseus.js";
363+
358364
export class GameScene extends Phaser.Scene {
359365
// (...)
360366
room: Room;
@@ -368,7 +374,7 @@ export class GameScene extends Phaser.Scene {
368374
// (...)
369375

370376
// listen for new players
371-
this.room.state.players.onAdd((player, sessionId) => {
377+
$(this.room.state).players.onAdd((player, sessionId) => {
372378
const entity = this.physics.add.image(player.x, player.y, 'ship_0001');
373379

374380
// keep a reference of it on `playerEntities`
@@ -386,7 +392,7 @@ When a player is removed from the state (upon `onLeave()` in the server-side), w
386392

387393
```ts
388394
// (...)
389-
this.room.state.players.onRemove((player, sessionId) => {
395+
$(this.room.state).players.onRemove((player, sessionId) => {
390396
const entity = this.playerEntities[sessionId];
391397
if (entity) {
392398
// destroy entity
@@ -448,9 +454,9 @@ Whenever the message is received in the server, we're going to mutate the player
448454
449455
```ts filename="MyRoom.ts"
450456
// (...)
451-
onCreate(options: any) {
452-
this.setState(new MyRoomState());
457+
state = new MyRoomState();
453458

459+
onCreate(options: any) {
454460
// handle player input
455461
this.onMessage(0, (client, payload) => {
456462
// get reference to the player who sent the message
@@ -487,14 +493,14 @@ We are going to use `.onChange()` since we need all the new coordinates at once,
487493
```ts
488494
// (...)
489495
// listen for new players
490-
this.room.state.players.onAdd((player, sessionId) => {
496+
$(this.room.state).players.onAdd((player, sessionId) => {
491497
const entity = this.physics.add.image(player.x, player.y, 'ship_0001');
492498

493499
// keep a reference of it on `playerEntities`
494500
this.playerEntities[sessionId] = entity;
495501

496502
// listening for server updates
497-
player.onChange(() => {
503+
$(player).onChange(() => {
498504
// update local position immediately
499505
entity.x = player.x;
500506
entity.y = player.y;

pages/tutorial/phaser/client-predicted-input.mdx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ title: "3: Client Predicted Input"
44
import { Callout, Cards, Tabs } from "nextra/components";
55
import { DownloadIcon, LinkIcon } from '@primer/octicons-react'
66

7-
<Callout type="warning">
8-
This tutorial still uses version 0.15. We're gradually migrating content to version 0.16.
9-
</Callout>
10-
117
# 3: Client Predicted Input
128

139
This guide will show you how you can build a multiplayer experience with Colyseus Multiplayer Framework and Phaser.
@@ -57,7 +53,7 @@ export class GameScene extends Phaser.Scene {
5753
Now, let's modify the `players.onAdd()` callback to identify which player is the current one, by checking the `sessionId` key against the connected `room.sessionId`:
5854
5955
```typescript
60-
this.room.state.players.onAdd((player, sessionId) => {
56+
$(this.room.state).players.onAdd((player, sessionId) => {
6157
const entity = this.physics.add.image(player.x, player.y, 'ship_0001');
6258
this.playerEntities[sessionId] = entity;
6359

@@ -70,15 +66,15 @@ this.room.state.players.onAdd((player, sessionId) => {
7066
this.remoteRef = this.add.rectangle(0, 0, entity.width, entity.height);
7167
this.remoteRef.setStrokeStyle(1, 0xff0000);
7268

73-
player.onChange(() => {
69+
$(player).onChange(() => {
7470
this.remoteRef.x = player.x;
7571
this.remoteRef.y = player.y;
7672
});
7773

7874
} else {
7975
// all remote players are here!
8076
// (same as before, we are going to interpolate remote players)
81-
player.onChange(() => {
77+
$(player).onChange(() => {
8278
entity.setData('serverX', player.x);
8379
entity.setData('serverY', player.y);
8480
});

pages/tutorial/phaser/fixed-tickrate.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ title: "4: Fixed Tickrate"
44
import { Callout, Cards, Tabs } from "nextra/components";
55
import { DownloadIcon, LinkIcon } from '@primer/octicons-react'
66

7-
<Callout type="warning">
8-
This tutorial still uses version 0.15. We're gradually migrating content to version 0.16.
9-
</Callout>
10-
117
# 4: Fixed Tickrate
128

139
This guide will show you how you can build a multiplayer experience with Colyseus Multiplayer Framework and Phaser.

pages/tutorial/phaser/linear-interpolation.mdx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ title: "2: Linear Interpolation"
44
import { Callout, Cards, Tabs } from "nextra/components";
55
import { DownloadIcon, LinkIcon } from '@primer/octicons-react'
66

7-
<Callout type="warning">
8-
This tutorial still uses version 0.15. We're gradually migrating content to version 0.16.
9-
</Callout>
10-
117
# 2: Linear Interpolation
128

139
This guide will show you how you can build a multiplayer experience with Colyseus Multiplayer Framework and Phaser.
@@ -35,7 +31,7 @@ On [Part 1](basic-player-movement) of this tutorial, we updated the player's vis
3531

3632
```typescript
3733
// listening for server updates
38-
player.onChange(() => {
34+
$(player).onChange(() => {
3935
// update local position immediately
4036
entity.x = player.x;
4137
entity.y = player.y;
@@ -58,7 +54,7 @@ To allow that, first we need to cache the latest player position received from t
5854

5955
```typescript
6056
// listening for server updates
61-
player.onChange(() => {
57+
$(player).onChange(() => {
6258
//
6359
// do not update local position immediately
6460
// we're going to LERP them during the render loop.

0 commit comments

Comments
 (0)