Jump to content

Position many sprites via array?


xronn
 Share

Recommended Posts

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

 

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

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);}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...