Issue arising from using setCollideWorldBounds and overlap in Phaser 3

I'm facing an issue with Phaser 3.

Whenever I use setCollideWorldBounds, I get an error saying "Cannot read property 'setCollideWorldBounds' of null" and the overlapping function doesn't seem to work. What's even more strange is that in my game config, the debug option is set to true, but it's only not visible in this particular sprite.

Game Config

export const config = {
  type: Phaser.AUTO,
  backgroundColor: "#125555",
  width: 1200,
  height: 900,
  scene: Battle,
  physics: {
    default: "arcade",
    arcade: {
      debug: true,
    },
  },
};

const game = new Phaser.Game(config);

Player.ts

 export class Player extends Phaser.Physics.Arcade.Sprite {
  constructor(scene: Phaser.Scene, texture: string) {
    super(scene, config.width / 2, config.height - 150, texture);
    scene.add.existing(this);
  }
....

My Scene

export class Battle extends Phaser.Scene {
  private player: Player;
  private enemy1: Enemy;
  private enemy2: Enemy;
  private enemy3: Enemy;
  constructor() {
    super("playGame");
  }
  preload() {
    this.load.image("background", "assets/space.png");
    this.load.image("player", "assets/playerShip.png");
    this.load.image("bullet", "assets/laserBullet.png");
    this.load.image("enemy1", "assets/enemy1.png");
    this.load.image("enemy2", "assets/enemy2.png");
    this.load.image("enemy3", "assets/enemy3.png");
  }

  create() {
    this.player = new Player(this, "player");
    this.player.setScale(0.09);
    this.player.setCollideWorldBounds(true);

    this.enemy1 = new Enemy(this, "enemy1");
    this.enemy2 = new Enemy(this, "enemy2");
    this.enemy3 = new Enemy(this, "enemy3");
    this.enemies = this.physics.add.group();
    this.enemies.add(this.enemy1);
    this.enemies.add(this.enemy2);
    this.enemies.add(this.enemy3);
    this.playerCollision = this.physics.add.overlap(
      this.player,
      this.enemies,
      this.hitPlayer,
      null,
      this
    );
  hitPlayer(player: Phaser.Physics.Arcade.Sprite, enemies) {
    this.resetEnemy(enemies);
  }

  resetEnemy(enemy) {
    enemy.y = 0;
    let randomX = Phaser.Math.Between(0, config.width);
    enemy.x = randomX;
  }
....

Answer №1

To resolve the issue, make sure to integrate the player into the physics of the scene. Implementing this step should resolve the error.

 class MainPlayer extends Phaser.Physics.Arcade.Sprite {
  constructor(scene: Phaser.Scene, texture: string) {
    super(scene, config.width / 2, config.height - 150, texture);
    scene.add.existing(this);
    scene.physics.add.existing(this);
  }

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

Convert an interface type variable to a string representation

I'm just starting to learn angular and I'm attempting to convert my response from an interface type into a string so that I can store it in session storage. However, when I try to save them, they are being stored as [object] in the session storag ...

Extending the type of parameters in TypeScript

I am trying to call a function from my UI library with a parameter type that extends the original (Suggestion) type by adding more properties. I found a resource that suggests it is possible here: https://github.com/Microsoft/TypeScript/issues/2225 (in the ...

What is the best way to iterate through a collection of two or more arrays in order to determine the total length of all

https://i.stack.imgur.com/PpFlB.pngI currently have multiple Arrays containing various inputs this.listNumber = [ { "GenericQuestions": [ { "input": "long", }, { "input": & ...

Unable to log in with password after hashing using React, NodeJS, and mySQL

I've implemented a salt and hash function to secure my password. Strange thing is, I can't log in using the original password, but it works when I use the hashed password from the database. Salt: "HashedPasswordCheck" Hash Function: function has ...

Tips for arranging various information into a unified column within an Antd Table

Is there a way to display multiple data elements in a single cell of an Ant Design table, as it currently only allows insertion of one data element? I am attempting to combine both the 'transactionType' and 'sourceNo' into a single cell ...

Ensure that a string contains only one instance of a specific substring

I need a function that removes all instances of a specific substring from a string, except for the first one. For example: function keepFirst(str, substr) { ... } keepFirst("This $ is some text $.", "$"); The expected result should be: This $ is some tex ...

Angular router for displaying multiple view groups

What is the most effective strategy for handling two groups of views in Angular? Let me explain how I typically structure my layout in app.component.html: <app-header></app-header> <router-outlet></router-outlet> <app-footer> ...

In Angular, set the default value of the first item in the list to be highlighted when the page loads

In my project, there are two key components: 1)contact and 2)display. The contact component is responsible for displaying a list of contacts as shown in the image below: https://i.sstatic.net/SnXFZ.png Next to the contact component, I have placed anothe ...

D3-cloud creates a beautiful mesh of overlapping words

I am encountering an issue while trying to create a keyword cloud using d3 and d3-cloud. The problem I am facing is that the words in the cloud are overlapping, and I cannot figure out the exact reason behind it. I suspect it might be related to the fontSi ...

Is it possible for TypeScript to mandate abstract constructor parameters?

This specific function is designed to take a constructor that requires a string argument and then outputs an instance of the constructed value T: function create<T>(constructor: new(value: string) => T): T { return new constructor("Hello& ...

Using Typescript to automatically infer strongly-typed recursive index types

Commencing with an Animal interface and a detailed map of animals residing on my farm: export interface Animal { species: string; edible: boolean; } export interface FarmMap{ [key: string]: Animal; } Everything seems to be running smoothly. Here ...

While utilizing Typescript, it is unable to identify changes made to a property by a

Here is a snippet of code I am working with: class A { constructor(private num: number = 1) { } private changeNum() { this.num = Math.random(); } private fn() { if(this.num == 1) { this.changeNum(); if(this.num == 0.5) { ...

What is the best method to create a TypeScript dictionary from an object using a keyboard?

One approach I frequently use involves treating objects as dictionaries. For example: type Foo = { a: string } type MyDictionary = { [key: string]: Foo } function foo(dict: MyDictionary) { // Requirement 1: The values should be of type Foo[] const va ...

Encountering issues with accessing a variable before its initialization in the getServerSideProps function in

Currently, I am working on setting up Firebase and configuring the APIs and functions to retrieve necessary data in my firebase.tsx file. Afterwards, I import them into my pages/index.tsx file but I am encountering an issue where I cannot access exports af ...

Ways to retrieve the most recent update of a specialized typing software

When attempting to run typings install in a sample project with the below typings.js file, I received a warning. How can we determine the latest version number and what does the number after the + symbol signify? { "globalDependencies": { "core-js ...

Connecting the list elements to the current state

My React component is defined as: export default class App extends React.Component<AppProps, Items> The Items class is declared as: export default class Items extends Array<Item> where Item is a class with various properties. When I direct ...

Tips for sorting through and minimizing data based on the most recent date

info = { start: 1, data: [ { name: 'Maria', date: '2020-02-15 }, { name: 'Paula', date: '2020-06-10 }, { name: 'Eva', date: '2020-12-05 }, { name: 'Sophia', date ...

What specific characteristic of TypeScript's number data type or the Math.ceil() function is responsible for this calculation mistake?

Currently, I am working on a function in Typescript that is supposed to generate a unique number each time it runs. However, there seems to be a problem with the arithmetic as the results are not always correct. Upon further examination of the code below, ...

Creating a custom autocomplete search using Angular's pipes and input

Trying to implement an autocomplete input feature for any field value, I decided to create a custom pipe for this purpose. One challenge I'm facing is how to connect the component displaying my JSON data with the component housing the autocomplete in ...

How to Implement a Pop-up Modal in Angular Using TypeScript

Looking to implement a popup window that activates when a specific button is clicked: <a (click)="open()" class='btn btn-primary m-r-5px'> <span class='glyphicon glyphicon-eye-open'></span> View </a> Utilize ...