I’m making the part of a level where a player sprite overlaps a telescope sprite, which should make the telescope ‘light up’ (just changes the frame from the spritesheet) and turn to normal when the player is no longer overlapping. This is what it looks like on screen:
Nah, that’s an invisible floor panel I’m using as an alternative to changing world boundaries. The telescope doesn’t seem to have a physics body despite me assigning it. It should just be a rectangle around it, right?
Firstly, You are adding the telescope as a static physics object, as you are passing true in, this.physics.add.existing(this.telescope, true);. This generally means that your telescope should not move. Since you are using overlap, you don’t really need it to be static anyway.
Secondly, you are setting the scale of the telescope, but you are not adjusting the size of the body. Unfortunately when you change the size of an image, the body does not adjust accordingly. Try using telescope.body.setSize(width, height) and telescope.body.setOffset(offsetX, offsetY) to match the sizes up. (You should probably set the telescope to start in the middle of the screen, so you can accurately position the body.)
Overall, you are not seeing the body because you are starting the telescope off the screen and as it moves in, only the image moves in because the body is static and staying put.
Thank you!
I removed this line from my code: this.physics.add.existing(this.telescope, true); and that has helped make the hitbox appear onscreen, I’ll just fix the offset, since it doesn’t appear around the sprite, but a bit higher and to the left of the desired location.
The one thing I’m struggling to access now is the frame of the telescope. this.telescope.frame isn’t right, so I’ve been looking for alternatives. The object is always using the same spritesheet, so this.texture.key doesn’t quite give me the desired output. Any idea how to access which frame is being used specifically? I realised I should use .setFrame() later on, but I can’t trigger it, as I can’t get the current frame.
Awesome! now it works fully!
This is in the create function:
//telescopes
this.telescope = new Telescope(this, 1500, 520, 'telescope',this.choose([0,4,8])).setOrigin(0.5,1).setScale(1.7);
this.physics.world.enable(this.telescope);
this.telescope.body.setSize(250, 250);
this.telescope.on("overlapstart", function() {
overlapping = true;
if (this.frame.name == 0){
this.setFrame(2);
}
if (this.frame.name == 4){
this.setFrame(6);
}
if (this.frame.name == 8){
this.setFrame(10);
}
});
this.telescope.on("overlapend", function() {
overlapping = false;
if (this.frame.name == 2){
this.setFrame(0);
}
if (this.frame.name == 6){
this.setFrame(4);
}
if (this.frame.name == 10){
this.setFrame(8);
}
});
This is in the update function:
// Treat 'embedded' as 'touching' also
if (this.telescope.body.embedded) {
this.telescope.body.touching.none = false
};
var touching = !this.telescope.body.touching.none;
var wasTouching = !this.telescope.body.wasTouching.none;
if (touching && !wasTouching) this.telescope.emit("overlapstart");
else if (!touching && wasTouching) this.telescope.emit("overlapend");