I'm making a game that involves Tetris, but I'm having trouble with getting the blocks to land. For some reason, the blocks are stopping just short of the long, clear sprite that I'm using as the ground.
The Code I'm using is as follows:
In the Create method:
this.active = false;
this.activeBlock = null;
In the Update method:
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.physics.add.collider(this.testblock, this.walls);
this.physics.add.collider(this.p1, this.testblock);
this.activeBlock = this.testblock;
this.active = true;
}
this.activeBlock.y = this.testblock.y + 0.1;
if(Phaser.Input.Keyboard.JustDown(this.keyA) && this.activeBlock.x != 352) {
this.activeBlock.x = this.activeBlock.x - 64;
}
if(Phaser.Input.Keyboard.JustDown(this.keyD) && this.activeBlock.x != 928) {
this.activeBlock.x = this.activeBlock.x + 64;
}
if (this.checkCollision(this.activeBlock, this.ground)){
this.activeBlock = null;
this.active = false;
}
And this is the checkCollision method I regularly use:
checkCollision(a, b) {
// simple AABB checking
if ((a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.height + a.y > b.y) ) {
return true;
}
else {
return false;
}
}
I'm not sure what I did wrong. Can someone tell?
If it helps, I'm using Phaser 3 in VSCode employing arcade physics.
Update:
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.setGravity(0.1);
this.physics.add.collider(this.walls, this.testblock, this.callbackOnCollision, null, this);
this.physics.add.collider(this.p1, this.testblock);
this.activeBlock = this.testblock;
this.active = true;
}
//this.activeBlock.y = this.activeBlock.y + 0.1;
callbackOnCollision(floor, block){
this.physics.world.disable( block)
this.activeBlock = null;
this.active = false;
}
This is the code I added after reading the suggestion.
It did get the blocks closer to the ground, but it still didn't reach the bottom. Also, the new line to control the block's gravity didn't work, as the blocks are now just affected by normal gravity.
Did I mess up in adding the code?