xronn Posted October 6, 2015 Share Posted October 6, 2015 Hey, I have an animation e.g. sea = this.add.sprite(410, 3700, 'sea');sea.animations.add('left', [6, 7, 8], 10, true);I want to place this sprite in loads of different positions is there a way I could store the x,y position in some sort of array or will I literally have to keep re-drawing the sprite e.g. sea = this.add.sprite(410, 3700, 'sea');sea1 = this.add.sprite(510, 3700, 'sea');sea2 = this.add.sprite(610, 3700, 'sea'); Link to comment Share on other sites More sharing options...
tips4design Posted October 6, 2015 Share Posted October 6, 2015 Never do that, always do DRY coding. Of course you can store positions in an array.var positions = [{x: 100, y:200}, {x: 300, y: 400} ];var sprites = [];for(var idx in positions) { var pos = positions[idx]; sprites.push(this.add.sprite(pos.x, pos.y, 'sea'));} Link to comment Share on other sites More sharing options...
jmp909 Posted October 6, 2015 Share Posted October 6, 2015 just to be clear, do you want to create 3 sprites at (410,3700), (510, 3700) and (610, 3700) ? nvm .. what he said ^^^ Link to comment Share on other sites More sharing options...
xronn Posted October 6, 2015 Author Share Posted October 6, 2015 Never do that, always do DRY coding. Of course you can store positions in an array.var positions = [{x: 100, y:200}, {x: 300, y: 400} ];var sprites = [];for(var idx in positions) { var pos = positions[idx]; sprites.push(this.add.sprite(pos.x, pos.y, 'sea'));} Hi, I've added the code and it works great thanks, but I'm not sure how to add an animation if I use the code below I get "Cannot read property 'add' of undefined, I guess I need to include something in the 'sprites' array but I'm not sure on the format. var waves_positions = [{x: 420, y:3700}, {x: 510, y: 3700} ]; waves_positions.animations.add('waves', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10, true); var sprites = []; for(var idx in waves_positions) { var pos = waves_positions[idx]; sprites.push(this.add.sprite(pos.x, pos.y, 'waves')); } Link to comment Share on other sites More sharing options...
tips4design Posted October 7, 2015 Share Posted October 7, 2015 Why are you trying to add the animations to the positions array? The `Array` Object doesn't have an add method. I think you want to do something like this:var waves_positions = [{x: 420, y:3700}, {x: 510, y: 3700} ];var sprites = [];for(var idx in waves_positions) { var pos = waves_positions[idx]; var sprite = this.add.sprite(pos.x, pos.y, 'waves'); sprite.animations.add('waves', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10, true); sprites.push(sprite);} jmp909 1 Link to comment Share on other sites More sharing options...
Recommended Posts