Referencing a sprite in a group

Recommended Posts

I have a player in a group like this:

    this.players = this.game.add.physicsGroup();

    blue_player = this.players.create(0, this.world.height - 490, 'blue_player');

    this.players.setAll('body.bounce.y', 0.2);
    this.players.setAll('body.gravity.y', 1000);
    this.players.setAll('collideWorldBounds', true);

    this.players.callAll('animations.play', 'animations', 'idle');

And I add a function to my game prototype that is called by my update function:

    blue_controls: function () {
      if (blue_keyMap.jump.isDown) {
        console.log("blue is going up in the world");
        this.players.blue_player.body.velocity.y = 2000;




And get: TypeError: Cannot read property 'body' of undefined

The error is with this line: this.players.blue_player.body.velocity.y = 2000;

I have tried just this.blue_player.body.velocity but get blue_player undefined, and others. I've ran out of things to try that I can think of.

I know I'm not referencing the object correctly, but I'm currently putting in the milage to learn object Inheritance and prototypes - and using this game I'm making as practice. I'm just a little stuck, help a noob out?

Share this post

Link to post
Share on other sites

If you're not going to set a variable to reference the player like using this.blue_player instead of blue_player then you'll have to work out which child it is in the group like this.players.children[0].body.velocity.y

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.