Draxy

Adding attributes to groups correctly

Recommended Posts

I want to make a group called players and then assign things to it like this.players.body.gravity.y = 1000;, etc

then make a red and blue player from that group that are identical but for the spreadsheets used, so i don't have to set gravity and other things of the sort twice for each.

Are groups the best way to do this?

I've tried with this code, but get "TypeError: Cannot read property 'bounce' of undefined" as an error

myGame.Game.prototype.create = function () {
    console.log("create is working");

    this.platforms = this.add.physicsGroup();
    this.platforms.enableBody = true;
    this.platforms.setAll('body.immovable', true);

    var ground = this.platforms.create(0, this.world.height - 64, 'ground');
    ground.scale.setTo(2, 2);
    ground.visible = false;

    var background = this.add.sprite(0, 0, 'background');
    background.animations.add("background", [0,1,2,3,4,5,6,7,8], 10, true);
    background.animations.play("background");
    background.width = this.game.width;
    background.height = this.game.height;
    this.world.sendToBack(background);  //used this to place background behind platforms

    this.players = this.game.add.group(); //make player group

    this.players.body.bounce.y = 0.2;  //give play group things so that red_player and blue_player
    this.players.body.gravity.y = 1000;//will have them
    this.players.body.collideWorldBounds = true;
    this.players.blue_player.animations.add('idle', [0, 1, 2], 2, true);
    this.players.animations.play('idle');

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

    this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).onDown.add(this.blue_jump, this);

};

My knowledge of objects is terrible, but I can see any problems with what I've done, can anyone help me out?

Thanks for reading.

Share this post


Link to post
Share on other sites

Hi, 

The problem is that your this.players is a group object, but A group object does not have a .body property and so you are trying to set the .bounce of something that does not exist. This is why you are getting an error. 
The procedure would be to first add your sprites to the group ,THEN set all the necessary physics properties:

this.players = this.game.add.physicsGroup(); //you need  physics group

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

//you can use setAll to set the same properties for every object in players

this.players.setAll('body.bounce.y',0.2);
this.players.setAll('body.gravity.y',1000);
//etc.....

//this will not work for the same reason (players does not have .blue_player property), but you already have a reference to the blue player so you can use that reference

    //this.players.blue_player.animations.add('idle', [0, 1, 2], 2, true);
   // this.players.animations.play('idle');

blue_player.animations.add('idle',[0,1,2],2,true);
this.players.callAll('animations.play', 'animations', 'idle);

You need to carefully consider which properties exist for certain objects and which ones do not (the manual is useful for this). Also, it makes more sense to use setAll if you have many objects in your group, but in your case you have only two objects (A red and blue player) and you even have a reference to them, so imho it makes more sense to just edit the properties of the red_player and blue_player:

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

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

red_player.body.bounce.y=0.2;
red_player.body.gravity.y=1000;
red_player.body.collideWorldBounds=true;
blue_player.body.bounce.y=0.2;
blue_player.body.gravity.y=1000;
blue_player.body.collideWorldBounds=true;
blue_player.animations.add(//etc...);
//etc..

 

Edited by samid737
bounce and gravity updated code

Share this post


Link to post
Share on other sites

Thanks for your reply @samid737, you helped me out a lot!

But now the physics seem broke, am I referencing the players right? My code now:

myGame.Game.prototype.create = function () {

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

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

    this.players.setAll('enableBody', true); //added this and the line below to see if I could get them to fall
    this.players.physicsBodyType = Phaser.Physics.ARCADE; 
    
    this.players.setAll('body.bounce', 0.2);
    this.players.setAll('body.gravity', 1000);
    this.players.setAll('collideWorldBounds', true);

    blue_player.animations.add('idle',[0,1,2],2,true);
    red_player.animations.add('idle',[0,1,2],2,true);
    this.players.callAll('animations.play', 'animations', 'idle');


};

 

Share this post


Link to post
Share on other sites
this.players.setAll('body.bounce.x', 0.2);
this.players.setAll('body.bounce.y', 0.2);
this.players.setAll('body.gravity.y', 1000);
this.players.setAll('body.collideWorldBounds', true);

body.bounce and body.gravity are Points, not numbers.

Share this post


Link to post
Share on other sites

You can remove these lines:

/* 'enableBody' is a Group property, not Sprite      */ this.players.setAll('enableBody', true);
/* already set by physicsGroup() (ARCADE is default) */ this.players.physicsBodyType = Phaser.Physics.ARCADE;

 

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.

Guest
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.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.