KevinnFtw Posted May 16, 2014 Share Posted May 16, 2014 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 More sharing options...
Heppell08 Posted May 16, 2014 Share Posted May 16, 2014 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 More sharing options...
KevinnFtw Posted May 16, 2014 Author Share Posted May 16, 2014 Well I see what you are doing there, however I'm already using most of the default properties on my sprites. Link to comment Share on other sites More sharing options...
jpdev Posted May 16, 2014 Share Posted May 16, 2014 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 thisvar 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} Dread Knight and webdva 2 Link to comment Share on other sites More sharing options...
KevinnFtw Posted May 16, 2014 Author Share Posted May 16, 2014 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 More sharing options...
jpdev Posted May 16, 2014 Share Posted May 16, 2014 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 webdva 1 Link to comment Share on other sites More sharing options...
Noid Posted May 17, 2014 Share Posted May 17, 2014 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 More sharing options...
Recommended Posts