Add more (custom) properties to sprite

Recommended Posts



I would like to know if it is possible to create more properties for a sprite.


For example,

I'm creating a multiplayer game that is based on GPS, so I would like to assign a latitude and longitude to a sprite.


So I would like to do it like this:

sprite.latitude = value;

sprite.longitude = value;


Is this possible out of the box, or do I have to modify the phaser.js to achieve this?


Thanks in advance,


Share this post

Link to post
Share on other sites

Would depend. The value of the longitude and latitude could be easily passed as an X and Y position which would be no issue at all. But depends on your approach really. For me it would be:

Longitude = 35;

Latitude = 120;

Sprite.x = Longitude;

Sprite.y = Latitude;

But you can also dynamically do that in your update loop.

Share this post

Link to post
Share on other sites

In javascript you can add properties to any object at any time.

So in your case you could just create the sprite and then do this

var sprite = game.add.sprite("player", 100, 100);sprite.customLongitude = 55;sprite.funnyVariableName = 20;

Now your sprite has those values, you can access them like any other value that it has out of the box.

Just be sure to only access values you actually added before. Otherwhise they will return "undefined".


But you can even check for that:


if (typeof sprite.blablabla === "undefined") {

//property was not assigned

} else {

//happily use blablabla


Share this post

Link to post
Share on other sites

Oh wow, is it really that simple? 


Man... how could I have not thought about that! :)


Will try it now, thanks for your answer and example! 


@jpdev, you are the man! Your answer is working great and saved me alot of time.

Thanks dude

Share this post

Link to post
Share on other sites

This does well enough for managing all my objects in my low-rez-dungeon for example. [see signature for link to the game, no sourcecode though]

(I even add those properties using the maps from the tiled-editor - because phaser can just take values from the tilemap and put them into custom spirte properties).


But If you want to be more organized and if you have a bunch of objects, that all have the same additional properties and you don't like the "I will just add them on the fly" way, then you should extend the phaser-sprite.


There is an example here:

Share this post

Link to post
Share on other sites

You can use sprite.hasOwnProperty('propname') to throw an error if sprite.propname is already defined, to make sure you're not overriding anything. That way, even if at some point in the future 'propname' becomes a default property of any Phaser.Sprite your game won't silently fail and you'll know what to fix.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.