fariazz

Adding a custom property to elements in a group

Recommended Posts

I was wondering if there is a way to add a custom property to all the sprites when creating a group, something like an "extend" property on the config object, similar to that of Scenes:


this.enemies = this.add.group({
    key: 'dragon',
    repeat: 5,
    setXY: {
      x: 120,
      y: 100,
      stepX: 70,
      stepY: 20
    },

    //proposed idea
    extend: {
      gold: 2,
      silver: 1
    }
  });

 

Is there a way currently to set a property to all the elements of a group? from the source code I only found Phaser.Action.Call which can be used for this.

Share this post


Link to post
Share on other sites

Nope, but it would be a simple and small add. Feel free to submit a PR, or just open an issue as a feature request.

Some thoughts though:

  1. Would it set the property if it didn't exist?
  2. If the property did exist, would it replace the current value, or skip it? (non destructive)

Share this post


Link to post
Share on other sites
19 hours ago, rich said:

Some thoughts though:

  1. Would it set the property if it didn't exist?
  2. If the property did exist, would it replace the current value, or skip it? (non destructive)

Not sure, I guess it would do whatever `extend` does for Scene objects.

Will have a look at how it's implemented for scenes and see what I can do :)

Share this post


Link to post
Share on other sites

Had a look at Phaser.GameObjects.Group#createFromConfig but couldn't think of an easy way to add something like what scenes have:

if (sceneConfig.hasOwnProperty('extend'))
        {
            for (var propertyKey in sceneConfig.extend)
            {
                newScene[propertyKey] = sceneConfig.extend[propertyKey];
            }
}

But also I'm not fully convinced this should be added. I kind of thought that it would be nice to have but then easily added sprite custom properties using Call on the group object in my game:

Phaser.Actions.Call(this.enemies.getChildren(), function(enemy) {
    enemy.speed = Math.random() * 2 + 1;
  }, this);

Maybe it's best not to add so many things and see what common use patterns are emerging first.

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.