Jendrik

How to load a random sprite out of a group?

Recommended Posts

Yeah, topic says all... ;-)

I´m trying to get a random png out of a group

like this:

var block = this.game.add.sprite(spawnPos, 416,GROUPNAME);

of course it´s not working. Anybody knows how to do it right?

Share this post


Link to post
Share on other sites

Hi,

 

there is no such a thing as  a group for an images (directory or image collection) in a Phaser by default,

 

this should work:

var exampleState = {  preload: function(){        game.load.image("redMonster", "assets/monsters/red.png");    game.load.image("greenMonster", "assets/monsters/green.png");    game.load.image("blueMonster", "assets/monsters/blue.png");  },  create: function(){    // array of loaded monsters:    var loadedMonsters = ["redMonster", "greenMonster", "blueMonster"];    // pick one name from an array    var monsterToload = this.game.rnd.pick(loadedMonsters);    // add picked monster to stage at 0,0    var monster = this.game.add.sprite(0, 0, monsterToLoad);  }}

if you are using MightyEditor you can access folders and images using mt helper. e.g.:

var exampleState = {  create: function(){    // array of loaded monsters    var loadedMonsters = Object.keys(mt.assets.monsters);    // pick one name from an array    var imageName = this.game.rnd.pick(loadedMonsters);    // image name might not be same as image key    // get asset key    var monsterToLoad = mt.assets.monsters[imageName].key;    // add picked monster to stage at 0,0    var monster = this.game.add.sprite(0, 0, monsterToLoad);}

by default project will have Loading folder and 2 images in it: preload.png and loading.png

// to access Loading folder: mt.assets.Loading;// to list contents:Object.keys(mt.assets.Loading);// to access image data located in the folder:mt.assets.Loading["loading.png"];

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.