Uncaught TypeError: a.setBodySize is not a function

I cannot apply the setBodySize property

Class ball:

export default class Ball extends Phaser.GameObjects.Sprite
{
	/**
	* @param {Phaser.Scene} scene
	* @param {number} x
	* @param {number} y
	* @param {string} texture
	*/
	constructor(scene, x, y, texture)
	{
		super(scene, x, y, texture)
		this.setScale(1.4)
		this.setBodySize(20, 20)
	}
}

Use ball:

this.balls = this.physics.add.group({classType: Ball, allowGravity: false, immovable: true})
this.balls.create(10, 10, 'ball')

You should extend Phaser.Physics.Arcade.Sprite.
A Phaser.GameObjects.Sprite doesn’t have a body. (only after physics.add)

1 Like

Now there is a mistake: Uncaught TypeError: Cannot read property ‘setSize’ of null

export default class Ball extends Phaser.Physics.Arcade.Sprite
{
	/**
	* @param {Phaser.Scene} scene
	* @param {number} x
	* @param {number} y
	* @param {string} texture
	*/
	constructor(scene, x, y, texture)
	{
		super(scene, x, y, texture)
		this.setScale(1.4)
		this.setBodySize(4, 4)
	}
}

I also tried to do this: this.setCircle(1)
Got an error: Uncaught TypeError: Cannot read property ‘setCircle’ of null

I output “this” and there is body in there

Add scene.physics.add.existing(this); before using physics (i.e. setBodySize).

1 Like

Thank you so much!!!:blush: