Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Displaying random sprite from Atlas


Rafaelx
 Share

Recommended Posts

I'm trying to add 1 out of the 5 images in atlas randomly, but they all show up one on top of the other, is there a way to fix this? Basically I want 1 of the sprites to display could be any of the 5 every time I run my level, but all I get is all 5 at the same time.

 

 


///Declaration

 this.load.atlas('Monsters', 'images/monsters.png', 'images/monsters.json');


////Where I call sprite

 this.figuritaspega = this.game.add.sprite(0, 0, 'Monsters');
        this.figuritaspega.frame = this.rnd.integerInRange(0,4);
         this.figuritaspega = this.game.add.group;
        this.figuraarriba = this.add.sprite(1015, 140, this.figuritaspega);
        this.figuraarriba.scale.set(0.9 , 0.9 );

////.json below

{"frames": [

{
	"filename": "amarillo.png",
	"frame": {"x":0,"y":0,"w":188,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":188,"h":200},
	"sourceSize": {"w":188,"h":200}
},
{
	"filename": "azul.png",
	"frame": {"x":188,"y":0,"w":240,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":240,"h":200},
	"sourceSize": {"w":240,"h":200}
},
{
	"filename": "naranja.png",
	"frame": {"x":428,"y":0,"w":162,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":162,"h":200},
	"sourceSize": {"w":162,"h":200}
},
{
	"filename": "rojo.png",
	"frame": {"x":590,"y":0,"w":190,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":190,"h":200},
	"sourceSize": {"w":190,"h":200}
},
{
	"filename": "rosa.png",
	"frame": {"x":780,"y":0,"w":231,"h":200},
	"rotated": false,
	"trimmed": false,
	"spriteSourceSize": {"x":0,"y":0,"w":231,"h":200},
	"sourceSize": {"w":231,"h":200}
}],
"meta": {
	"app": "http://www.codeandweb.com/texturepacker",
	"version": "1.0",
	"image": "monsters.png",
	"format": "RGBA8888",
	"size": {"w":1011,"h":200},
	"scale": "1",
	"smartupdate": "$TexturePacker:SmartUpdate:41785e106df91b6daf42364753f15c41:5fca3c08999ac8d93eabfac98fafaf65:8fc4d3ec51ba7bc700054b5f64cf62b1$"
}
}

 

Edited by Rafaelx
edited tile I was wrong on old title.
Link to comment
Share on other sites

You can provide a frame id (number) instead of frame name (string) when creating your sprite, so this should do what you want:

this.figuritaspega = this.game.add.sprite(0, 0, 'Monsters', this.rnd.integerInRange(0,4));

Also there are problems with the rest of your code. This line wont create a group, it just passes the group creation function reference to the figuritaspega variable:

this.figuritaspega = this.game.add.group;

It should be:

this.figuritaspega = this.game.add.group();

And the rest won't work too, because you try to pass a group to the key parameter of this.add.sprite function:

this.figuraarriba = this.add.sprite(1015, 140, this.figuritaspega);
this.figuraarriba.scale.set(0.9 , 0.9 );

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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