"Experience a thrilling rendition of Tetris with Phaser 3 clone - watch as the game freezes when attempting to

Currently in my Tetris clone project, I am focused on working with single blocks and attempting to stack them in rows. Below is the code snippet for handling these blocks:

if (this.active == false){
    this.cube = Math.floor((Math.random()* 7));
    this.testblock = this.physics.add.sprite(608, 32, this.blocks[this.cube]);
    this.testblock.body.immovable = true;
    this.testblock.body.allowGravity = false;
    //this.testblock.body.setGravityY(-100);
    this.physics.add.collider(this.walls, this.testblock);
    this.physics.add.collider(this.p1, this.testblock);
    this.activeBlock = this.testblock;
    this.active = true;
}

this.activeBlock.y = this.activeBlock.y + 0.5;

if(Phaser.Input.Keyboard.JustDown(this.keyE)){
    console.log(this.activeBlock.y);
}

// More code follows .......

The game functions properly until the second block lands, at which point it freezes. Upon inspection, an error message displays "Cannot read properties of null (reading 'y')", specifically pointing to the line:

if (this.activeBlock.y == 672)

This line should trigger the landing process for the first row, where a block already occupies that space. It seems like the logic to trigger the landing process for the second row isn't executing.

Even though the block passes the y-coordinate threshold for the second row (608), and there are no apparent instances of setting the active block to null, the issue persists. Can anyone provide insight into what might be causing this unexpected behavior?

Answer â„–1

The primary challenge is relatively simple to address and correct (although it may not completely resolve all potential issues, considering the specific coding style employed). Upon reviewing your code and explanations, it appears that the initial falling Cube is placed within this if block:

if (this.activeBlock.y == 608){
   ...
   this.activeBlock = null; // "this" is the culprit
   ...
}

Within this segment, this.activeBlock is being set to null. Therefore, after resetting this.activeBlock, the subsequent check of if (this.activeBlock.y == 672) triggers the reported error, not following the creation of the second block.

Resolution: Utilize else if or/and incorporate an additional validation before accessing the property of this.activeBlock, such as

if (this.activeBlock && this.activeBlock.y == 672)
. (for ALL if(this.activeBlock.y ...) conditions)

Tip: It is preferable to verify/check rather than assume/know current values (as human errors can occur).

By the way: It is beneficial/best practice to verify the existence of an object (is not null) before accessing its properties.

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

Razor View does not support the activation of Javascript

I've been struggling to get some Javascript to work on a Razor View page, but for some reason it's not being activated. Any help would be greatly appreciated! Just a heads up: The View has a shared layout that loads jQuery & Bootstrap for all vi ...

The function Waterline.create() is not available

Currently in the process of building a basic REST API using Sails.js and Waterline-ORM, I've encountered an issue regarding Post.create is not a function when trying to create an object within the ORM on a Post request. Here is my model: module.expor ...

Why does the method Array.concat on an extended class remove empty values in Node.js version 8.9.3?

I am experimenting with adding new functionality to Arrays in node.js without altering the original Array class to avoid any unintended consequences in other modules. During testing, I encountered some failures which led me to discover that the behavior o ...

Is there a way to link table A's ID to table B's userID in a postgreSQL database?

Is it possible in PostgreSQL to establish a relational index between table A ID and table B userId for the purpose of joining two tables based on their ids? To provide further clarification, here is an example using MongoDB and Mongoose: const Billing = ...

The Django image upload is not successful when attempting to use JavaScript from a string of markup code

I have successfully implemented django dynamic forms that work upon clicking, but I am facing an issue with uploading images. I made sure to include request.FILES and added enctype="multipart/form-data", however, the image upload still doesn't seem t ...

Jquery display function experiencing unresponsiveness

Currently, I am trying to implement some show/hide functionality in my JavaScript file: $(document).ready(function() { $('#me').hide(); $('#send').click(function() { $('#me').show("slow"); }); }); Strange ...

Parsing URLs with Node.js

Currently, I am attempting to parse the URL within a Node.js environment. However, I am encountering issues with receiving null values from the following code. While the path value is being received successfully, the host and protocol values are returnin ...

Utilizing ng-route to parse the URL and identify the corresponding controller by its name

I am implementing ng-click and ng-view to navigate my div id= "listings" in order to display various mobile listings including iphone 4, iphone 5, nexus 4, nexus 5, nexus 6, lg g3, and more. You can check out my code on Plunkr. <div ng-repeat = ' ...

`A selection box populated with JSON information``

Looking to populate a dropdown with JSON data using jQuery. { "Name":["A","B","C"], "Movie":["X","Y","Z"] } Here is the script I have implemented so far: $("#firstbox").change(function(){ var $selection=$(this); $.getJSON("d ...

regex: sequences containing a dot in the center

Struggling with a regex challenge - need to validate two words with a mandatory point between them. No special characters, no @ symbol, and no spaces allowed. The format should be like this: test.test I've been attempting to use the following regex ...

Lack of communication between Node.js modules

Currently, I am diving into the world of node.js as part of a personal project to enhance my skills. To maintain best practices, I have been segmenting my code into different modules. However, I have hit a roadblock where a module I created is not communic ...

Is it possible to use Three.js to generate a single mesh that can replace multiple individual objects?

Check out my latest project at this link: maison.whiteplay.fr I'm currently working on creating a 3D PacMan game. In my code, I am using mesh to construct the level, but I'm facing performance issues due to the large number of individual bubble ...

How can I use jQuery to fix the positioning of a right-side div?

How can I make the right hand side div fixed within row two, so that it stays in place when scrolling down the page and moves up when reaching the footer area? Here is an example of my code: I would like to keep the col-md-4 class fixed when it reaches t ...

Is there a way for me to retrieve a variable from within a .then function nested within another .then function?

Here is an example of code where I am attempting to log the value of 'a' to the console. driver.sleep(2000).then(function logAInConsole() { var a = ["Quas","Wex","Exort","Invoke"]; for(var i = 0; i < a.length; i++) { driver.sleep( ...

After the element is re-rendered, the React ref becomes null

My task management system includes a to-do list where tasks can be either a single big textarea (referred to as dataArea) or a list of those textareas. The goal is for these textareas to dynamically grow in height as content is added, achieved by setting t ...

Iterating Through Multiple Dimensions

I am facing a challenge with creating rules from three arrays that I have. The task is to generate every possible combination of entries from each array, but I am struggling with the logic required to write a function for this purpose. For example: var ar ...

Having trouble fetching JSON data using Ajax in Flask

I am working on creating a straightforward web application for displaying logs. The backend is powered by Python 3.4 and Flask, while the frontend consists of a simple web form with AJAX integration. Flask: import json from flask import Flask, jsonify, r ...

Setting a specific time zone as the default in Flatpickr, rather than relying on the system's time zone, can be

Flatpickr relies on the Date object internally, which defaults to using the local time of the computer. I am currently using Flatpickr version 4.6.6 Is there a method to specify a specific time zone for flatpickr? ...

Incorporating momentum into Laravel using webpack

It has come to my attention that the moment JS library is experiencing a known issue when attempting to require ./locale. Unfortunately, the problem remains unresolved and it is recommended to seek out solutions from various tickets. While I am open to imp ...

Using globs to specify files for gulp.src

I'm facing a challenge in setting up a glob array for my JavaScript concatenation build task within the Gulp workflow. The file structure I am working with looks like this: ├── about │ └── about.js ├── assets ├── contact â ...