Navigation

  • Recent
  • Tags
  • Users
  • Search
  • Login
Colyseus
  • Login
  • Search
  • Recent
  • Tags
  • Users

Documentation GitHub

We're migrating to GitHub Discussions. This forum does not accept new registrations since April 6, 2023.
  1. Home
  2. kayyos
  3. Posts
  • Profile
  • More
    • Continue chat with kayyos
    • Flag Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

Posts made by kayyos

RE: Issue with phaser tutorial

I can not believe I didn't noticed the version mismatch!

Updating both server and client to 0.15 has done the job.

Thankyou for your time

posted in Questions & Help • 2 Apr 2023, 04:12
Issue with phaser tutorial

Hi, I've been following the Colyseus Phaser tutorial, my issue is in relation to step 7: https://learn.colyseus.io/phaser/1-basic-player-movement.html#listening-for-state-changes

I am receive the error: TypeError: this.room.state.players.onAdd is not a function

My client side create():

	async create() {
		console.log("Joining room...");

		try {
			this.room = await this.client.joinOrCreate("my_room");
			console.log("Joined Room");
		} catch (e) {
			console.error(e);
		}
		this.room.state.players.onAdd((player: any, sessionId: any) => {
			//
			// A player has joined!
			//
			console.log("A player has joined! Their unique session id is", sessionId);
		});
	}

My server side RoomState:

// MyRoomState.ts
import { MapSchema, Schema, type } from "@colyseus/schema";

export class Player extends Schema {
  @type("number") x: number;
  @type("number") y: number;
}

export class MyRoomState extends Schema {
  @type({ map: Player }) players = new MapSchema<Player>();
}

And lastly (the relevant parts of) OnJoin:

  onJoin(client: Client, options: any) {
    console.log(client.sessionId, "joined");
    const mapWidth = 200;
    const mapHeight = 300;

    // create Player instance
    const player = new Player();

    // place Player at a random position
    player.x = (Math.random() * mapWidth);
    player.y = (Math.random() * mapHeight);

    // place player in the map of players by its sessionId
    // (client.sessionId is unique per connection!)
    this.state.players.set(client.sessionId, player);
  }

Any guidance appreciated !

posted in Questions & Help • 26 Mar 2023, 09:07

© 2023 Endel Dreyer