Jump to content

How do you guys do inheritance for phaser entities?


Fishrock123
 Share

Recommended Posts

"non-standard" in what sense? there is no "standard" way of doing "class inheritance" in javascript, as it is not directly supported by the language syntax, but the way that Phaser does so is as standard as any (i.e. very commonly used). 

 

there are two examples in the 'examples/sprites' directory showing how to extend the Sprite class ("extending sprite demo 1" and "extending sprite demo 2"). they ought to get you started.

Link to comment
Share on other sites

Oh. In example one the MonsterBunny extends the Sprite. Since sprite is the basic entity container in phaser (do not like.) this is the way it should be done, no?

In the game examples, entity properties are just tacked onto generated sprites in a haphazard, messy manner. :/

Shouldn't the game examples being doing sprite extension? >_<
 

"game.add.existing" - That is what I needed. Weird naming though. It doesn't really exist yet in the gameworld.

Link to comment
Share on other sites

I don't recall the game examples adding any fields to the sprites. perhaps I missed something last I looked at them.

 

adding data (fields & methods both) directly to objects as needed is pretty standard practice in javascript (and other dynamic languages), where "duck typing" generally supersedes using type hierarchies. some javascript experts frown on using 'extension'/inheritance at all (e.g. Douglas Crockford, "JavaScript: The Good Parts"), prefering the prototypical object pattern and using closures to create private data.

 

each to their own, I say, and javascript supports both - though the syntax may be ugly.

Link to comment
Share on other sites

"game.add.existing" is not for adding things that exist in the game world, it is for adding things that exist in code...

You can create a new Sprite with

var spr = new Phaser.Sprite(game, x, y, "imagekey");

and then add it to the game world with

game.add.existing(spr);
Link to comment
Share on other sites

Oh. In example one the MonsterBunny extends the Sprite. Since sprite is the basic entity container in phaser (do not like.) this is the way it should be done, no?

In the game examples, entity properties are just tacked onto generated sprites in a haphazard, messy manner. :/

Shouldn't the game examples being doing sprite extension? >_<

 

"game.add.existing" - That is what I needed. Weird naming though. It doesn't really exist yet in the gameworld.

 

I'm pretty sure none of the game examples tack any additional properties onto Sprites that don't already exist. It's not something I ever do in practise. If I need to extend a Sprite beyond what it contains then I'll create a specific object for it and add the properties there.

 

The examples should show the most simplest method possible, which they do. We could do with an advanced tutorial that does it more conventionally, i.e. doesn't use functions, uses States, uses extended objects - i.e. the way we all write actual games. But this is too complex to learn the basics from and we've not yet had time to create some.

 

I absolutely loath frameworks that enforce you use a specific faux OO style of coding. I would much rather provide something that allows you to code that way, if you so wish, but doesn't enforce it. Although if you're the first to be trying it that way you are probably flying on the edge right now until it's more explored and documented.

 

You don't have to use the GameObjectFactory at all btw, there's nothing stopping you creating objects directly. Just ensure you add them to the world in the same way the factory does. It's a convenience function, not a requirement.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...