Jump to content

Enable/disable hitboxes on frame change


Recommended Posts



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.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);


    setFrame(frame) {
        this.frame = frame;
        this.hitBoxForFrame0.enableBody = this.frame === 0;
        this.hitBoxForFrame1.enableBody = this.frame === 1;
     this.bullets, this.mySprite.hitBoxes, function(bullet, hitBox) {
     }, null, this


Link to comment
Share on other sites


  • Recently Browsing   0 members

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