Jump to content

Add more (custom) properties to sprite


KevinnFtw
 Share

Recommended Posts

Hi,

 

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,

Kevin

Link to comment
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.

Link to comment
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

}

Link to comment
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! 

 

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

Thanks dude

Link to comment
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:  http://examples.phaser.io/_site/view_full.html?d=sprites&f=extending+sprite+demo+1.js&t=extending%20sprite%20demo%201

Link to comment
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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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