Draxy

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);

    blue_player.animations.add('idle',[0,1,2],2.1,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

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.