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')
Milton
2
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
Milton
6
Add scene.physics.add.existing(this);
before using physics (i.e. setBodySize).
1 Like