Jump to content

Cannot use physics on a class that extends Phaser.Physics.Arcade.Sprite


mattbrand
 Share

Recommended Posts

Hey all!

I have a class that looks like this:

export default class Bubble extends Phaser.Physics.Arcade.Sprite
{
    private text:Phaser.GameObjects.Text;
    private velocity:Phaser.Math.Vector2;
    private dragging:Boolean;
    private game:Game;

    constructor(scene, game, x, y)
    {
...
}

I want to set up physics on these objects (the bubbles). I've tried 2 ways to do this.

1. Set up the physics directly in the class constructor itself:

this.setCollideWorldBounds(true);
this.setVelocity(100, 100);
this.setBounce(1);

2. Set up in my game's main scene where I instantiate the bubbles:
 

let bubble = new Bubble(this, this.game, x, y);
bubble.setVelocity(100, 100);
bubble.setCollideWorldBounds(true);
bubble.setBounce(1);
this.bubbleGroup.add(bubble);
this.physics.overlap(bubble, this.bubbleGroup, this.bubbleHitBubble);

Neither of these work. I get "TypeError: Cannot read property 'setVelocity' of null".

Why can I not set physics properties on the bubbles if they extend Phaser.Physics.Arcade.Sprite?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...