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

Phaser foreach


xronn
 Share

Recommended Posts

Hi,

 

I was playing around with health I tried to add a foreach statement but its completely unreferenced, 

 

foreach (hero.health){   game.add.sprite(10,10, 'health');}

You probably get the idea what I was trying, I knew if it did work all the sprites would have been stacked on top but I wasn't sure how to do a "health bar".

 

Thanks!

Link to comment
Share on other sites

I never tried doing it like that so I worked out that if you have a group and parent it to the player, add sprites in and set the X and Y to either above or below the player. Then in the group I declared 5 seperate vars so each could be removed at a given time based on an update call for player health.I looked for other ways but couldn't find any but that method works fine for me. :)

Link to comment
Share on other sites

Its a nice guide but again you use a randomX, then just increment the number in the update function. I want to create a number of the same elements in locations I decide like this:

        fireGroup = this.add.group();        fire = fireGroup.create(272, 180, 'fire');        fire = fireGroup.create(320, 180, 'fire');

But this isn't the right way and even with groups the only way I know to create the same element in different fixed locations is to change the variable name.

Link to comment
Share on other sites

I want the sprites to be fixed to the game world. I'm creating fire lanterns that have there own animation and would like some dotted around my map to make it look nice. So I was thinking of creating a fireGroup and then adding each one of lanterns to the group but of course I can't just have them spawn randomly I want to place them in specific locations. Thats what I was hoping to do anyway :(

Link to comment
Share on other sites

That can be done but to be honest, the way to do that would be to use a group, add the lanterns in the group. An example could be:

firegroup = game.add.group();

var fire1= firegroup.create(X,Y,'fire');

var fire2=firegroup.create(X,Y,'fire');

Etc etc along them lines. Just keep adding them like that and you can have any amount aslong as you declare a new var per new fire like I did above. The X and the Y are game world related because in the creating of the groupe there is no parent like player or anything so the game world is the relevant X and Y position of the lanterns created in the group. That is a basic run down of the groups. To animated just create an animation and play the animation in the update from what you create. So you could have:

var fire1 = firegroup.create(100,100,'fire');

fire1.animations.add('fire1burning',[0,1,2], 10, true);

var fire2 = firegroup.create(150, 100, 'fire');

fire2.animations.add('fire2burning', [0,1,2], 10, true);

Then do the same for each lantern and naming each animation like I did above so you can play them all in the update. There probably is a simpler way to animate a full group but I don't know that so I use the method I've just posted. It works well too. Give it a go and see anyway :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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