Amawuff Posted February 24, 2014 Share Posted February 24, 2014 Maybe I'm just overlooking it or not understanding the documentation properly, but what is the easiest way to access the property of a child in a group? game_state.main.prototype = { preload: function() { // Function called first to load all the assets this.game.load.image('coil','assets/coil.png') }, create: function() { // Fuction called after 'preload' to setup the game this.game.stage.backgroundColor = '#2d2d2d'; //style for text var style= { font: "15px Helvetica", fill: "#ffffff"}; //make coils group this.coils = game.add.group(); this.coils.createMultiple(2, 'coil'); this.add_coil(100, 100); this.add_coil(100, 400); }, update: function() { // Function called 60 times per second //coils collide with eachother, avoids overlap. this.game.physics.collide(this.coils); }, add_coil: function(x,y) { //Get the first dead coil of our group var coil = this.coils.getFirstDead(); //Set the initial position of the coil coil.reset(x, y); //change physics body to circle so its edges are detected. coil.body.setCircle(25); //set initial coil properties coil.charge = 0; coil.chargeRate = 2; coil.chargeMax = 100; coil.chargeMin = 0; //make coils draggable coil.inputEnabled = true; coil.input.enableDrag(true); },};for example if I wanted to pull the value of a child coil's charge how would i do that? Link to comment Share on other sites More sharing options...
rich Posted February 25, 2014 Share Posted February 25, 2014 Keep a reference to the child and then just access that? Or get it from the Group somehow (by name, index, first alive/exists, etc). Link to comment Share on other sites More sharing options...
Amawuff Posted February 25, 2014 Author Share Posted February 25, 2014 Thanks Rich, I guess thats what i was wondering. how would I get it from the group? I could use first alive to grab the first one, but what about the second? Link to comment Share on other sites More sharing options...
jpdev Posted February 25, 2014 Share Posted February 25, 2014 You could access all your childrens doing this in the update function for example:this.coils.forEach( function(coil) { coil.velocity.x = 100; });(or use forEachAlive etc.) Link to comment Share on other sites More sharing options...
rich Posted February 26, 2014 Share Posted February 26, 2014 You could actually use a cursor for this: /** * The cursor is a simple way to iterate through the objects in a Group using the Group.next and Group.previous functions. * The cursor is set to the first child added to the Group and doesn't change unless you call next, previous or set it directly with Group.cursor. * @property {any} cursor - The current display object that the Group cursor is pointing to. */ Link to comment Share on other sites More sharing options...
Recommended Posts