espace Posted October 10, 2016 Share Posted October 10, 2016 hi, how do you access the sprite number 2 in this case ? https://jsfiddle.net/espace3d/sncxypzz/ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create }); function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); game.load.image('rect', 'http://s10.postimg.org/6fa6mgrd5/motif.jpg'); } //E for external module var E = E || {} MonsterGroup = function (game, image, action) { Phaser.Group.call(this, game); for (var i = 0; i < 3; i++) { var sprite = this.create(game.world.randomX, game.world.randomY, image); if (action == 'bounce') { game.add.tween(sprite).to({ y: sprite.y - 100 }, 2000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); } else if (action == 'slide') { game.add.tween(sprite).to({ x: sprite.x + 200 }, 4000, Phaser.Easing.Elastic.Out, true, 0, 1000, true); } } }; MonsterGroup.prototype = Object.create(Phaser.Group.prototype); MonsterGroup.prototype.constructor = MonsterGroup; E=E || {} function create() { customGroup1 = new MonsterGroup(game, 'rect', 'bounce'); //this don't work // customGroup1.sprite[2].scale.x=.5 } Link to comment Share on other sites More sharing options...
samme Posted October 10, 2016 Share Posted October 10, 2016 You can use group.getChildAt(2) // or group.children[2] espace 1 Link to comment Share on other sites More sharing options...
espace Posted October 11, 2016 Author Share Posted October 11, 2016 Thanks, but if i want to attach different elements to this group (sprite and circle in this case), how do you recognize them and acces them? https://jsfiddle.net/0cuku02e/ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create }); function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); game.load.image('rect', 'http://s10.postimg.org/6fa6mgrd5/motif.jpg'); } //E for external module var E = E || {} MonsterGroup = function (game, image,image2) { Phaser.Group.call(this, game); for (var i = 0; i < 3; i++) { var sprite = this.create(game.world.randomX, game.world.randomY, image); var circle = this.create(game.world.randomX, game.world.randomY, image2); } }; MonsterGroup.prototype = Object.create(Phaser.Group.prototype); MonsterGroup.prototype.constructor = MonsterGroup; E=E || {} function create() { customGroup1 = new MonsterGroup(game, 'rect','circle'); } Link to comment Share on other sites More sharing options...
espace Posted October 13, 2016 Author Share Posted October 13, 2016 Nobody ? Link to comment Share on other sites More sharing options...
drhayes Posted October 13, 2016 Share Posted October 13, 2016 If you know which sprite you need to reference, why not keep a reference to it directly? Like this.circle1 or this.circle2? Link to comment Share on other sites More sharing options...
espace Posted October 13, 2016 Author Share Posted October 13, 2016 hi, but sorry i don't see how ...because sprite is not defined or sprite1 is not defined or sprite[1] is not defined https://jsfiddle.net/espace3d/xajfLbmq/ Link to comment Share on other sites More sharing options...
drhayes Posted October 14, 2016 Share Posted October 14, 2016 Inside your loop you could do something like "this['sprite' + i] = this.create..." and then you'd have access to them. Or, unroll the loop and give each one a real name like "this.cat = " or "this.dog = ". Link to comment Share on other sites More sharing options...
espace Posted October 14, 2016 Author Share Posted October 14, 2016 i achieve like this : https://jsfiddle.net/espace3d/p926traa/ but when i do this.sprite my sprite have only one position and not random position how do you do with "this" ? sorry for this noob question. var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create }); function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); game.load.image('rect', 'http://s10.postimg.org/6fa6mgrd5/motif.jpg'); } //E for external module var E = E || {} var sprite={} var circle={} MonsterGroup = function (game, image,image2) { Phaser.Group.call(this, game); for (var i = 0; i < 10; i++) { sprite[i] = this.create(100+i, game.world.randomY, image); // don't work //this.sprite[i] = this.create(100+i, game.world.randomY, image); sprite[i].scale.setTo(.2,.2) circle[i] = this.create(game.world.randomX, game.world.randomY, image2); } }; MonsterGroup.prototype = Object.create(Phaser.Group.prototype); MonsterGroup.prototype.constructor = MonsterGroup; E=E || {} function create() { customGroup1 = new MonsterGroup(game, 'rect','circle'); customGroup1.alpha=1 sprite[1].alpha =.2 } Link to comment Share on other sites More sharing options...
espace Posted October 15, 2016 Author Share Posted October 15, 2016 Anybody ? Link to comment Share on other sites More sharing options...
drhayes Posted October 17, 2016 Share Posted October 17, 2016 Once you create a sprite by saying "this.sprite[0]" or "this.sprite[1]" you have to keep using "this" when you refer to it. And you have to define an empty array before the loop, like "this.sprite = [];". espace 1 Link to comment Share on other sites More sharing options...
espace Posted October 17, 2016 Author Share Posted October 17, 2016 solved thanks https://jsfiddle.net/8ujhhowb/ Link to comment Share on other sites More sharing options...
espace Posted October 26, 2016 Author Share Posted October 26, 2016 difference in time prototype vs normal solution ! prototype : minuteur démarré_display:80 prototype : 2.53 ms_display:84 normal : minuteur démarré_display:86 normal : 2.92 ms >> 15% more fast for prototype https://jsfiddle.net/htqsso8x/ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:preload, create: create }); function preload() { game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png'); game.load.image('rect', 'http://s10.postimg.org/6fa6mgrd5/motif.jpg'); } //E for external module var E = E || {} //var sprite={} var circle={} MonsterGroup = function (game, image,image2) { Phaser.Group.call(this, game); this.sprite=[] for (var i = 0; i < 5; i++) { this.sprite[i] = this.create(100+i, game.world.randomY, image); this.sprite[i].scale.setTo(.2,.2) } //return sprite }; MonsterGroup.prototype = Object.create(Phaser.Group.prototype); MonsterGroup.prototype.constructor = MonsterGroup; E=E || {} function create() { console.time("prototype") customGroup1 = new MonsterGroup(game, 'rect','circle'); console.timeEnd("prototype") console.time("normal") var group1= game.add.group() var sp={} for (var i = 0; i < 5; i++) { sp[i]= game.add.sprite(10,game.world.randomY,'rect') sp[i].scale.setTo(.1,.1) group1.add(sp[i]) } console.timeEnd("normal") } Link to comment Share on other sites More sharing options...
Recommended Posts