I’m trying to set sprite hitboxes to match the size of the graphics. I currently have this:
As you can see, the hitbox size doesn’t match.
I know that you have to set the hitbox size separately using setSize()
and setOffset()
, so I’m using this here in my code:
create(){
this.coordinates = {
//key: [x,y,scale]
0:[20, 20, 0.5],
1:[60, 60, 0.8],
2:[100, 100, 1],
3:[140, 140, 1.2],
4:[180, 180, 0.3],
5:[220, 220, 2],
6:[260, 260, 0.7],
7:[300, 300, 1.7],
}
this.astros = this.physics.add.group();
this.scatterObjects(this.coordinates);
}
scatterObjects(coords){
let allFrames = this.anims.generateFrameNumbers('junk');
allFrames.forEach(element => {
console.log(element.frame);
console.log(coords[element.frame][0]);
let thing = new Astro(this,coords[element.frame][0],coords[element.frame][1],'junk',element.frame)
thing.setScale(coords[element.frame][2]);
console.log(thing);
//This line causes issues:
// thing.setSize(45*coords[element.frame][2], 60*coords[element.frame][2]);
this.astros.add(thing);
});
I get the following error:
I don’t really understand how that can happen, since the object I’m calling this setSize()
function on is printed right above and it is certainly not null…
Any help would be apperciated