LuisM Posted March 28, 2018 Share Posted March 28, 2018 I'm making a game with some collectible objects. I created a group for each type of object so I know what type is the object I collected. but I'm in need of a hint how I can arrange this! I already have 6 groups. and each group has 4 to 7 items that are scattered throughout the scene. would you have a better way to do that? thanks! Link to comment Share on other sites More sharing options...
onlycape Posted March 28, 2018 Share Posted March 28, 2018 Hi @LuisM , You can add a custom property to the elements of a group. Example: sprite.grouptype = 'type1'; Link to comment Share on other sites More sharing options...
LuisM Posted March 29, 2018 Author Share Posted March 29, 2018 16 hours ago, onlycape said: Hi @LuisM , You can add a custom property to the elements of a group. Example: sprite.grouptype = 'type1'; Thank you. @onlycape I'm doing something I need to collect items and did not want to make a group for each item plus a single clip and put different types inside but give up! I'll let each group be the type of items so it gets better. about your tip I do not know where I would use this my knowledge of javascript is low. but that's how my group looks. gStones= game.add.group(); game.physics.p2.enable(gStones); gStones.enableBody = true; gStones.physicsBodyType = Phaser.Physics.P2JS; gStones.create(320, 510, 'stone1').scale.setTo(0.7); gStones.create(520, 710, 'stone2').scale.setTo(0.7); gStones.create(720, 310, 'stone3').scale.setTo(0.7); gStones.setAll('body.kinematic', true); just in case I need to give types to each item in a group can I explain where and how to use it? Link to comment Share on other sites More sharing options...
Andy Williams Posted March 29, 2018 Share Posted March 29, 2018 5 hours ago, LuisM said: Thank you. @onlycape I'm doing something I need to collect items and did not want to make a group for each item plus a single clip and put different types inside but give up! I'll let each group be the type of items so it gets better. about your tip I do not know where I would use this my knowledge of javascript is low. but that's how my group looks. gStones= game.add.group(); game.physics.p2.enable(gStones); gStones.enableBody = true; gStones.physicsBodyType = Phaser.Physics.P2JS; gStones.create(320, 510, 'stone1').scale.setTo(0.7); gStones.create(520, 710, 'stone2').scale.setTo(0.7); gStones.create(720, 310, 'stone3').scale.setTo(0.7); gStones.setAll('body.kinematic', true); just in case I need to give types to each item in a group can I explain where and how to use it? Thanks! It's useful Link to comment Share on other sites More sharing options...
onlycape Posted March 29, 2018 Share Posted March 29, 2018 Hi @LuisM , With this code I have created a new property for the group elements named customProperty (test code in phaser sandbox here ) : function create() { var sprite = game.add.sprite(0, 0, 'phaser'); var gStones= game.add.group(); // Adding elements using a variable named "element" var element = gStones.create(320, 510, 'phaser'); element.scale.setTo(0.7); //Here I create the new property "customProperty" element.customProperty = 'type1'; // Reusing variable "element" I create other element element = gStones.create(520, 710, 'phaser'); element.scale.setTo(0.7); element.customProperty='type2'; //And one more time element = gStones.create(720, 310, 'phaser'); element.scale.setTo(0.7); element.customProperty = 'type3'; //Testing customProperty with forEach (this method iterates each group's element) gStones.forEach(function(item){ console.log(item.customProperty); }); } I hope it helps you.. Suerte !! Link to comment Share on other sites More sharing options...
Recommended Posts