[SOLVED] disableBody is not a function

I am trying to make a game where a player (called serol) is catching tetrominos (tetris pieces) falling from the sky. I made a group for the tetrominos, which currently consists of one tetromino. There is an overlap function which manages when the player and the tetrominos overlap.

I expect this: When the player touches a tetromino, the tetromino disappears
What actually happens: When the player touches a tetromino, the game crashes and I get the following error in the console Uncaught TypeError: tetromino.disableBody is not a function

If anyone has any idea what is missing from my code, please give me a heads up :slight_smile:

class Level1 extends Phaser.Scene {
	constructor() {
		super("level1");
	}
  grav = 40;
	create() {
    //background
    console.log('Loading bg...');
    this.lvl1Bg = this.add.image(0,0,"lvl1Bg").setOrigin(0,0);
    //floor platform
    this.stagePlatform = this.add.tileSprite(config.width/2, 640, 0, 0, 'stage').setOrigin(0.5, 0.8);
    this.physics.add.existing(this.stagePlatform, true);
    this.stagePlatform.enableBody = true;
    this.stagePlatform.body.immovable = true;

    //spawning serol
    this.serol = new Serol(this, 250, 50);
    this.physics.add.existing(this.serol);
    this.serol.anims.play('staticBob',true);
    //colliding with floor platform
    this.physics.add.collider(this.stagePlatform, this.serol);
    
    //enabling serol controls
    this.cursorKeys = this.input.keyboard.createCursorKeys();
    this.serol.setCollideWorldBounds(true);

    //spawning tetrominos
    this.tetrominos = this.physics.add.group();
    this.physics.world.enable(this.tetrominos);

    this.tet1 = new Tetromino(this, 360, 50);
    this.physics.add.existing(this.tet1);
    this.tet1.body.setAllowGravity(false);
    this.tet1.body.moves = true;

    this.tetrominos.add(this.tet1);

    //enabling collisions between serol and tetrominos
    this.physics.add.overlap(this.serol, this.tetrominos, this.catchTetromino, null, this);

    
  }

	update() {
    this.movePlayerManager();
    this.tetFall(this.tet1, 20);
  }
  
  movePlayerManager(){
    if (this.cursorKeys.left.isDown) {
      this.serol.setVelocityX(-gameSettings.playerXSpeed);
      this.serol.anims.play('walkLeft',true);
    }
    else if(this.cursorKeys.right.isDown) {
      this.serol.setVelocityX(gameSettings.playerXSpeed);
      this.serol.anims.play('walkRight',true);
    }
    else {
      this.serol.anims.play('staticBob',true)
      this.serol.setVelocityX(0);
    }
    if(this.cursorKeys.up.isDown && this.serol.body.onFloor()) {
      this.serol.setVelocityY(-gameSettings.playerYSpeed);
    }
  }
  //falling tetrominos
  tetFall(tetromino, accel) {
    tetromino.body.setAcceleration(0,accel);
    if (tetromino.y > config.height) {
      this.tetReset(tetromino);
    } else if (tetromino.y < 0){
      this.tetReset(tetromino);
    }
    if (tetromino.x > config.width) {
      this.tetReset(tetromino);
    } else if (tetromino.x < 0){
      this.tetReset(tetromino);
    }
  }
  tetReset(tetromino) {
    tetromino.y = 0;
    var randomX = Phaser.Math.Between(0, config.width);
    tetromino.x = randomX;
    tetromino.setFrame(Phaser.Math.Between(0, 59));
  }
  catchTetromino(serol,tetromino){
    tetromino.disableBody(true, true);
  }
}

/* Serol Class */
class Serol extends Phaser.Physics.Arcade.Sprite {
  // healthBar
  constructor(scene, x = 0, y = 0, texture = 'serol') {
    super(scene, x, y, texture)

    scene.add.existing(this)
    scene.physics.add.existing(this)

    scene.events.on('update', this.update, this)
  }
  update() {
    // this.lives.follow(this)
  }
}

/*tetromino class*/
class Tetromino extends Phaser.GameObjects.Sprite {
  constructor(scene, x=0, y=0, texture = 'tetromino', frame = Phaser.Math.Between(0, 60)) {
    super(scene,x,y,texture,frame)
    scene.add.existing(this)
    scene.events.on('update', this.update, this)
  }
}
}

Hi @ThePandaJam,
In your code, the objects of “Tetromino” class don’t have body. You need to extend that class from a physics class like you did with “Serol” class.
Regards.

1 Like

Thank you! That worked like a charm :slight_smile:

class Tetromino extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x=0, y=0, texture = 'tetromino', frame = Phaser.Math.Between(0, 60)) {
    super(scene,x,y,texture,frame)
    scene.add.existing(this)
    scene.events.on('update', this.update, this)
  }
}