Different collision box for player sprite and weapon sprite

So I have been messing with Phaser 3 and for my second game wanted to try doing a simple dungeon crawler to get experience with combat and collision boxes.

What I have is a sprite with a knight and an image of sword and want to build an object that combines the two. It is however important that they have different collision boxes. I have looked around and I see that containers are recommended when trying to attach sprites so I tried them but they don’t seem to allow for two different collision boxes.

I have come up with a solution for this by creating my own custom game object:

player

knight

weapon

I do wonder if this is just and over-complicated solution. Is this the way to go or have I missed some kind of feature that allows me to do exactly what I want here.

Also, by the way, what is the type variable that is asked by the GameObject super method?

I think you can do this with a physics-enabled container and children.

The type argument and property is used internally. But you probably shouldn’t extend Phaser.GameObjects.GameObject by itself.

1 Like

I find dealing with containers to be quite confusing. A container can have a body (collision box), I use that as the player sprite body. Now I need a new collision box that would turn on when I attacked with my sword and I am not quite sure how I would go about that. I tried looking for examples of action oriented games on phaser but did not find much besides long distance combat (bullets and other kind of projectiles). Looking at the examples perhaps phaser is not the tool for this kind of games…

You can toggle the body.enabled of the sword (and also visible) when the attack starts and ends.