Separate Sprite and GameObject


Even if I didn’t have any reply to my proposition of making a collective boilerplate, I started working on it. I’ve already made a topdown and a side-scroller example. They both mostly use the same code, making features available on both version. On both example, you can talk to non playable characters, you can shoot fireball while pressing CTRL, you’ll have to avoid monsters, there are level entrances and exits… 90% of the code is common between the two games.

The next feature I want to add is spine skeletal animation for the different characters: player, npcs and enemies. On a normal project, I would have simply change the parent class from Phaser.Physics.Arcade.Sprite to SpineGameObject. But I want the boilerplate to be versatile and be able to switch easily from different sprites without having to change the character class.

What I am thinking to do is to make the character class to extend directely from Phaser.GameObjects.Zone and simply attach them a sprite that would follow them everywhere. So if I want to use SpineGameObject instead of a Sprite, I can simply change the attached sprite, and that wouldn’t affect in any case the character behaviour.

Since it is a boilerplate, I want to start on solid ground and use an efficient solution for such an important thing, so it will be interesting for other people to use that boilerplate. But I don’t know Phaser enough to know if this is the most efficent solution. Phaser might already deal with such an issue. So do you think it is the solution I should use or is there a better one?

Thanks a lot!

Is there a lot of phaser devs using spine?

Well, there is me. I kickstarted Spine back in 2014.
So I definitely want my boilerplate to be able to use it :wink:

And if there aren’t, there should be more, it is a really efficient way to create animated character.

But it is not only about SpineGameObject. Characters could also be a shape or another kind of game object, event if I have to admit that most could be easily emulated with a Sprite.

Also, character physics and behaviours are common in many games and we can create archetype that the developer can use. It is the same for the character sprites. They are character aspects that we can isolate easily. If we keep them both in separate classes, it will be easier to combine them.

PS: I’ve just tried K438-B and Revenge of Acharis, it is really fun :slight_smile:

Thanks for trying my games, i really appreciate :slight_smile: they are old now and made for one month gamejams, i’m rewriting K438-B from scratch :slight_smile:
For spine objects, if you use them, add them :slight_smile:

Are you a long way rewriting K438-B? As I said before, I am still interested by making a swiss army boilerplate and are still looking for other developpers to work with. If you are still starting, that could be an option…

Yes, i started a while ago, it’s my long term project

Well, it worths a try. Good luck with it then!

I wouldn’t extend Zone, because the two game objects would be unrelated.

Character could just be its own class, with a sprite property holding the game object.

The character class actually needs to be a game object, since it will deal with all the physics. It needs a body with a size, coordinates and velocity, so it can moves and interact with other element in the scene, as other character, but also tiles, hitzones, etc… If I have to access the character sprite everytime I need to use physics, it will complexify the code.

There is currently another active thread called Phaser on Node.js and Yannick example is actually doing what I want since the physics has been separated of the rendering. In this case, it is because the first one has to be done server side while the other one is done client side, but the result is the same.

For the physics part, the player class extends Phaser.Physics.Arcade.Sprite and no texture is sent to the constructor.

For the rendering part, the player class extends Phaser.GameObjects.Sprite.

Would that work for you? At least, the two game objects would be related, one having the physics and the other one no. But is it correct to create an arcade sprite with no texture?

In my mind, the character wouldn’t even be aware of its sprite. I was thinking of setting up a controler class whose only purpose is to add/destroy the right sprite every time a character is added/destroyed and to even their coordinates.

I’m not sure it is clear, it is sometime hard to explain certain concepts in a foreign language.

As i make almost only metroidvanias, i use a Player class that extends Phaser.GameObjects.Sprite, and state machine to handle all the possibilities. I think there isn’t one way of doing a platformer, it really depends on what you want to achieve.
It doesn’t stop me from changing the texture if necessary.

That’s fine. My point was you can’t attach a second Game Object to a Zone in a rendering sense; it’s not a Container.

@BlunT76 Thanks. On this other post, rexrainbow said he was interested to make a series of plugins for plaformers, as he did for board games, but he wasn’t familiar with what kinds of function the platformer should include. Since you have experience with them, don’t hesitate to contact him if this interest you. I would also be interested to see some of your code, since you often find good ideas in other people code.

@samme I wasn’t using the proper word, since attach already have a meaning in Phaser. I was simply thinking about linking them in my own code. I’ve never thought about making my character class a Container. It actually makes a lot of sense, since I can attach any kind of Sprite to it. That could also be useful for other features like health bar. I’ll have to think about the impact on performances but that’s a good option. Thanks!

I have a problem for syncing character and sprite position, as you can see it in the following codepen:

The two objects (here a rectangle and a sprite) have the same coordinates and the same origin. If I don’t resize any of the objects, they are perfectly aligned. But if I change the size of the sprite (in order to change the hitbox size), the sprite is displayed a little above its coordinates and the only solution I found is modify its offset. I spent hours and hours looking for a solution, with no luck. Does someone know how I can change the body size and still have it aligned correcly according to its coordinates? Thanks a lot!