AGoodUsername Posted March 10, 2016 Share Posted March 10, 2016 Say I had a group: this.group = game.add.group(); and I spawned sixteen elements, one after another. How would I get the first one spawned? this.group.getAt(0); only works half of the time. Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 10, 2016 Author Share Posted March 10, 2016 Something like this.lines.getFirstAlive();?? Link to comment Share on other sites More sharing options...
nkholski Posted March 10, 2016 Share Posted March 10, 2016 I'd guess that this.group.children is in the order the children was added (firstElement = this.group.children[0]) but I haven't tried. Otherwise, or if I use the group as a pool which would mess up the order anyway, I would add a custom property to the group. If I really only needed the last: this.group.add(element); this.group.firstElement = this.group.firstElement ? this.group.firstElement : element; If I wanted to keep track of all: this.group.add(element); this.group.elementOrder.push(element); // this.group.elementOrder is an array. // Get the first one: firstElement = this.group.elementOrder[0]; // Get the first one out of cue and put the second one as first: firstElement = this.group.elementOrder.pop(0); Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 10, 2016 Author Share Posted March 10, 2016 11 hours ago, nkholski said: I'd guess that this.group.children is in the order the children was added (firstElement = this.group.children[0]) but I haven't tried. Otherwise, or if I use the group as a pool which would mess up the order anyway, I would add a custom property to the group. If I really only needed the last: this.group.add(element); this.group.firstElement = this.group.firstElement ? this.group.firstElement : element; If I wanted to keep track of all: this.group.add(element); this.group.elementOrder.push(element); // this.group.elementOrder is an array. // Get the first one: firstElement = this.group.elementOrder[0]; // Get the first one out of cue and put the second one as first: firstElement = this.group.elementOrder.pop(0); Sorry if I'm doing something wrong, but it says this.group.elementOrder and firstElement are undefined. I tried this.lines.children[0].kill();, but it still has a problem; sometimes it kills, the first, sometimes the second. Now, my guess to fix this would be to use .pop()? Link to comment Share on other sites More sharing options...
nkholski Posted March 10, 2016 Share Posted March 10, 2016 Yes. It would be custom properties. You need to add them, like this "mygroup.firstElement = null;" or "mygroup.elementOrder = []". If you use the latter your would pop it to "firstElement" as in my example then kill it with "firstElement.kill();". Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 10, 2016 Author Share Posted March 10, 2016 25 minutes ago, nkholski said: Yes. It would be custom properties. You need to add them, like this "mygroup.firstElement = null;" or "mygroup.elementOrder = []". If you use the latter your would pop it to "firstElement" as in my example then kill it with "firstElement.kill();". this.lines.firstElement = []; this.lines.elementOrder = []; and this.lines.elementOrder.push(this.lines); this.lines.firstElement = this.lines.elementOrder[0]; this.lines.firstElement = this.lines.elementOrder.pop(0); this.lines.firstElement.kill(); are the new lines I added. However, it gives me the error this.lines.firstElement.kill is not defined. Link to comment Share on other sites More sharing options...
nkholski Posted March 10, 2016 Share Posted March 10, 2016 First, I had two suggestions, just a single reference or an array to keep track of the order of all children. I think you can skip the "firstElement" property and go for the array with "elementOrder". I guess that "this.lines" is a group? What you do is pushing a refrence to the group itself. // 1. Make a group and add custom property "elementOrder": this.lines = this.game.add.group(); this.lines.elementOrder = []; // 2. Make a new line (a sprite in this example): var line = this.game.add.sprite(x, y, 'spritesheet'); // 3. Add it to the group this.lines.add(line); // 4. Push it to elementOrder: this.lines.elementOrder.push(elementOrder); // 5. Get first element and kill it: (in my previous example I showed both how to keep the element, and to pop it. But you probably want to pop it) var firstElement = this.lines.elementOrder.pop(0); firstElement.kill(); 2-4 could be repeated anytime in your code, adding unlimited number of lines and still keep track of the order. (With modification you could use a pool, reusing lines and just push the references) Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 11, 2016 Author Share Posted March 11, 2016 3 hours ago, nkholski said: First, I had two suggestions, just a single reference or an array to keep track of the order of all children. I think you can skip the "firstElement" property and go for the array with "elementOrder". I guess that "this.lines" is a group? What you do is pushing a refrence to the group itself. // 1. Make a group and add custom property "elementOrder": this.lines = this.game.add.group(); this.lines.elementOrder = []; // 2. Make a new line (a sprite in this example): var line = this.game.add.sprite(x, y, 'spritesheet'); // 3. Add it to the group this.lines.add(line); // 4. Push it to elementOrder: this.lines.elementOrder.push(elementOrder); // 5. Get first element and kill it: (in my previous example I showed both how to keep the element, and to pop it. But you probably want to pop it) var firstElement = this.lines.elementOrder.pop(0); firstElement.kill(); 2-4 could be repeated anytime in your code, adding unlimited number of lines and still keep track of the order. (With modification you could use a pool, reusing lines and just push the references) I'm spawning the lines with this.ray = this.lines.getFirstDead(); this.ray.scale.setTo(2, 1); this.ray.tint = 16777215; this.ray.reset(101, 20); this.ray.angle = -90; this.ray.body.angularVelocity = 100; because I want to clone them, so is this //kill function this.lines.elementOrder = []; this.lines.elementOrder.push(this.ray); var firstElement = this.lines.elementOrder.pop(0); firstElement.kill(); all right? Link to comment Share on other sites More sharing options...
nkholski Posted March 11, 2016 Share Posted March 11, 2016 The first two lines in the kull function don't belong there. Add this just once when creating the group: this.lines.elementOrder = []; Put this in your spawn function: this.lines.elementOrder.push(this.ray); Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 11, 2016 Author Share Posted March 11, 2016 18 minutes ago, nkholski said: The first two lines in the kull function don't belong there. Add this just once when creating the group: this.lines.elementOrder = []; Put this in your spawn function: this.lines.elementOrder.push(this.ray); lines group: this.lines = game.add.group(); this.lines.enableBody = true; this.lines.physicsBodyType = Phaser.Physics.ARCADE; this.lines.createMultiple(50, 'line'); this.lines.elementOrder = []; spawning lines: r = Math.random(); if(r < 0.51){ this.ray = this.lines.getFirstDead(); this.lines.elementOrder.push(this.ray); this.ray.scale.setTo(2, 1); this.ray.tint = 16777215; this.ray.reset(101, 20); this.ray.angle = -90; this.ray.body.angularVelocity = game.rnd.integerInRange(100, 125); } kill function: var firstElement = this.lines.elementOrder.pop(0); firstElement.kill(); It kills the second one spawned if there are two. Thanks for your time, I know how frustrating it must be Link to comment Share on other sites More sharing options...
nkholski Posted March 11, 2016 Share Posted March 11, 2016 Sorry, my bad. Use shift() instead of pop() and it should work. AGoodUsername 1 Link to comment Share on other sites More sharing options...
AGoodUsername Posted March 11, 2016 Author Share Posted March 11, 2016 14 hours ago, nkholski said: Sorry, my bad. Use shift() instead of pop() and it should work. THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (digital hug)(spam emote) Link to comment Share on other sites More sharing options...
Recommended Posts