Jump to content

Enable/disable hitboxes on frame change


MonsieurBlutbad
 Share

Recommended Posts

Hello,

 

I have a Sprite with 2 different frames which require different hitboxes for collision handling. I have created a group which consists of multiple sprites that form all hitboxes. Now I want to simply turn them on and off, depending on the frame of the parent Sprite. I thought this could be achieved with setting enableBody on the hitboxes to true or false. But it doesn't work. All hitboxes are active all the time. Any suggestions on how to make this work? Or is my approach wrong?

 

export default class MySprite extends Phaser.Sprite {
    constructor(game, x, y) {
        super(game, x, y, 'my_sprite_key');
        this.game = game;
        this.game.physics.arcade.enable(this);
        this.game.add.sprite(this);

        this.enableBody = true;
        this.body.immovable = true;

        this.hitBoxes = this.game.add.group();
        this.hitBoxes.enableBody = true;

        this.hitBoxForFrame0 = this.hitBoxes.create(0, 0);
        this.hitBoxForFrame0.body.setSize(this.width * 0.5, this.height, -this.width, -this.height);

        this.hitBoxForFrame1 = this.hitBoxes.create(0, 0);
        this.hitBoxForFrame1.body.setSize(this.width * 0.5, this.height, -this.width * 0.5, -this.height);

        this.addChild(this.hitBoxes);
        this.setFrame(1);
    }

    setFrame(frame) {
        this.frame = frame;
        this.hitBoxForFrame0.enableBody = this.frame === 0;
        this.hitBoxForFrame1.enableBody = this.frame === 1;
    }
}
 this.game.physics.arcade.overlap(
     this.bullets, this.mySprite.hitBoxes, function(bullet, hitBox) {
         console.log('Hit');
     }, null, this
);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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