Exploring Multi-Level TileMaps in Phaser 3 with JSON

My game currently only restarts on the same tilemap, even when the key is set to level-2.

I'm trying to figure out the best way to implement multiple levels. I attempted a solution where "level-2" was designated as the key, but the tilemap failed to update.

Would it be more logical to create a new stage for each level? I tried that approach, but ran into issues with importing the player in game.ts.

Is there a simpler way to solve this by simply restarting the scene?

game.ts :

import { Scene } from "phaser";

import Scenes from "./";
import { Player } from "../objects";
// Additional code continues...

gameManager :

import { Scene } from "phaser";
import { Player } from "../objects";
import Scenes from "../scenes";
// More code follows...

Answer №1

Great job on your code! The only thing that seems out of place is setting this.currentLevel = 1; in the constructor. If you want the first level to be called, you should set this.currentLevel = 0; instead.

Make sure to move that line(s) into the create function.

this.physics.add.collider(this.propsLayer, this.player, (player, tile) => {
  if ((tile as Phaser.Tilemaps.Tile).index == 37) {
    console.log("Collision with tile index 37");
    GameManager.instance.nextlevel();
    GameManager.instance.currentLevelKey;
    this.scene.restart();
  }
}); 

It's important to note that this.physics.add.collider adds an eventHandler action, which should only be done once. The update function gets called multiple times.

Tip: Creating a minimal viable example can make debugging easier. Check out this guide from StackOverflow for more information: here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

the reason for the blurring of shadows in Three.js

Typically, the shadow appears as shown below: https://i.sstatic.net/5L9N2.png However, upon adjusting the parameters of directionalLight.shadow.camera directionalLight.shadow.camera.left = -50 directionalLight.shadow.camera.right = 50 directionalLight.s ...

Is there a common method for generating complex identifiers to be used as the HTML element's id, class, or name attributes

Is there a recommended method for "encoding" complex identifiers such as {Source:"ARCH.1", Code: "456-789.456 A+", SubNumber:##2} to be used in HTML elements' id, class, and name attributes? I could come up with something myself, but perhaps there is ...

Steps to Validate a Form: To allow the submit button to be enabled only when all input fields are filled out; if any field is left empty,

https://i.sstatic.net/AHlJX.png Is it possible to enable the send offer button only after both input boxes are filled? I'm sharing my code base with you for reference. Please review the code and make necessary modifications on stackblitz 1. exampl ...

When trying to authorize my channel, the JSON data is coming back as a blank string

I've encountered an issue with my JavaScript code: Pusher is throwing the error message "JSON returned from auth endpoint was invalid, yet status code was 200. Data was: ", indicating empty data. I have double-checked the broadcasting service provider ...

Submitting a form in Codeigniter with AJAX causes unnecessary posts due to the presence of a jQuery cookie

Having an issue with the $.cookie jQuery plugin causing my form to submit prematurely. The form code is as follows: <?php echo form_open($this->uri->uri_string(), 'class="ajax_email_submission"'); ?> <div id="email_new ...

Is Using Web Workers for AJAX Calls an Over-Optimization?

Currently, I am working on a code that manages all AJAX requests using Web Workers (where available). These workers are mainly handling the XMLHttpRequest object without any additional computations. All requests initiated by the workers are asynchronous (r ...

Sending Uint8Array buffer without any null bytes

I am attempting to transfer image data from the Jimp image object to Tesserract (OCR library) using a buffer: image.getBufferAsync('image/png').then((buffer) => { // Buffer here is <Buffer 12 34 56 ... const worker = new TesseractWorke ...

Exploring RESTful routing using ui-router

I am currently in the process of creating a small AngularJS application. I have created two separate views: one for displaying a list of all employees, called employeeListView, and another for showing detailed information about a specific employee, known a ...

Is it possible to trigger an event in Laravel prior to the expiration of a user's login session?

To keep track of when a user logs out, I have set up an event that is triggered after clicking the logout button. The code below demonstrates how I store the logout time: <?php namespace App\Listeners; use App\UserTrack; use Carbon\Carb ...

Tips for receiving dual return values from an AJAX request

I am sending an array of table IDs to retrieve the table numbers associated with those IDs from the database. I need to add up all the default seats for each table ID and return the total. JAVASCRIPT : function showUser(str) { if ...

Definition of Stencil Component Method

I'm encountering an issue while developing a stencil.js web component. The error I'm facing is: (index):28 Uncaught TypeError: comp.hideDataPanel is not a function at HTMLDocument. ((index):28) My goal is to integrate my stencil component i ...

What is preventing the onmouseup event from being effective?

In my recent research, I came across some interesting information on mouse-click events from w3schools. According to them, the onmousedown, onmouseup, and onclick events are all crucial parts of a mouse-click process. It starts with the onmousedown event b ...

React Native failing to properly link with Redux

As a newcomer to the realm of React Native, I am facing challenges when it comes to integrating Redux with React Native and getting it to function properly. Once the JavaScript bundle is completed, I encounter errors such as "can't read property type ...

Designing Interactive Circular Dates on Website

Currently, I am working on a webpage using HTML, CSS, JavaScript, and PHP. The goal is to design a page that features 7 circles representing the current date and the next 6 days. Users should be able to click an arrow to navigate to the following 7 days. ...

Can anyone explain how to conduct a live search using Ajax?

Hello, I'm currently working on a project where I've successfully connected a large database to a website and implemented an AJAX live search feature. However, I am now in need of a non-live version of the AJAX search. The current setup displays ...

Terminate the execution of the process.exec function

Currently, I have a function in my code that is responsible for executing a specific process. Here's how it looks: static async runTest() { await process.exec(`start ${currentDir}/forward.py`); } runTest(); Here's the thing – once this Python ...

Promise.all() ensures that all promises in the array are resolved with the same value

Currently, I'm in the process of developing a module that contains a function designed to return a promise using Promise.all() for the purpose of sending emails to multiple users. By simplifying the code, I have managed to isolate and reproduce the is ...

The jQuery draggable feature ceases to function after it has been dropped

I have a scenario with two divs, each housing a list of quantities and items. These items are draggable, and the div containing them is droppable. The condition here is if an item with the same name exists in the div, it cannot be dropped on that div again ...

Issue with HTTP Interceptor not being effective when making service calls

I've implemented an interceptor to automatically add headers to each HTTP request without manual intervention. However, I'm facing an issue where the service call inside my interceptor is not triggering for some reason. Below is the code snippet: ...

What are some effective strategies for ensuring the user remains logged in when they come back using the same device?

As part of my PWA development, I utilize PHP, AJAX, and JS. One of my key objectives is to ensure that the user's logged-in status remains persistent when they return to the PWA application. Currently, I achieve this by utilizing an Access token store ...