Size is not the same between setDisplaySize and setSize

I have set the same parameters for setSize and setDisplaySize, but the display size is larger than the size as a result. Is that an error?

class GameObject extends Phaser.Physics.Arcade.Sprite {
    constructor(scene, tt, config) {
        super(scene, unit.width(config.x), unit.height(config.y), tt);
        this.config = config;
        scene.add.existing(this);
        scene.physics.add.existing(this);
        this.body.setImmovable(true);
        this.setSize(unit.height(config.w), unit.height(config.h));
        this.setDisplaySize(unit.height(config.w), unit.height(config.h));
        if (config.collide) {
            scene.physics.add.collider(scene.player, this);
        }
    }
    resize() {
        this.setPosition(unit.width(this.config.x), unit.height(this.config.y));
        this.setDisplaySize(unit.height(this.config.w), unit.height(this.config.h));
        if (this.config.collide) {
            this.setSize(unit.height(this.config.w), unit.height(this.config.h));
        }
    }
}

:wave:

Don’t use setSize() with Image, Sprite, etc.

You can use setBodySize(), but you probably don’t need to. The body is also scaled by setDisplaySize().

1 Like

Thanks for your help, I wanted to ask you a question of mine. In GameObject class I don’t need setSize then body size still change with display size. But in my Player class it’s not like that, I tried many values but it doesn’t solve the problem. Until I accidentally forgot to pass the parameter to setSize, it worked again, the body size was equal to the display size again. I find it difficult to understand.

        scene.add.existing(this);
        scene.physics.add.existing(this);
        this.setGravityY(unit.height(config.gravity));
        this.setDisplaySize(unit.height(this.size), unit.height(this.size));
        this.setSize();

I was wrong. This is actually the inconsistent performance of the Body and StaticBody classes in Physics.

My create Image and create Sprite both use the same logic. But after calling setScale and then calling body.setSize, the body.size of the Sprite is always larger than the displaySize, and the performance on the Image object is normal.

See Arcade.Body#setSize(). The units are unscaled pixels, so like width and height, not displayWidth and displayHeight.