Jursdotme

Cant get group to work

Recommended Posts

So im trying to create enemies in my little starter game (im new to this). I have added my monsters as objects in "Tiled" and do the following:

    monsters = this.add.group();
    monsters.enableBody = true;
    map.createFromObjects(
        "Monsters",
        "blob",
        { key: "monster" },
        0,
        true,
        false,
        monsters
    );

    monsters.forEach(function(enemy) {
        enemy.setBounceX(1);
        enemy.setCollideWorldBounds(true);
        enemy.body.velocity.x = 40;
    }, this);

However, this fails at the "monsters.forEach()" section. I have seen this done a hundred times all across the web and i have tried any incarnation under the sun. It always fails at this point.

 

Tried solutions with basically the same result:

https://thoughts.amphibian.com/2015/12/enemies-that-do-something.html

I have been stuck on this for hours. Please help.

Share this post


Link to post
Share on other sites

First, that amphibian tutorial is great but it's for Phaser 2 not 3 so it may be causing some issues there.

Second, we will need some more info. When you say fail do you mean you get an error, or does nothing happen? Do you see the monster sprites on the screen?

Right off the bat it looks like the parameters in your createFromObjects call may be wrong. I think you are using the Phaser 2 example which won't work. See the Phaser 3 examples and docs:

  1. https://labs.phaser.io/edit.html?src=src\game objects\tilemap\static\create from objects.js
  2. https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tilemap.html#createFromObjects__anchor

 

Also, it doesn't look like your repo is public.

Share this post


Link to post
Share on other sites

You have to avoid the Phaser 2 examples, they won't work and will confuse you. https://medium.com/@michaelwesthadley/modular-game-worlds-in-phaser-3-tilemaps-1-958fc7e6bbd6 etc. will be better help.

var monsters = this.physics.add.group();

monsters.addMultiple(map.createFromObjects(
  'Monsters',
  'blob',
  { key: 'monster' },
));

monsters.getChildren().forEach(function (enemy) {
  // You need to use the `body` methods because these are Sprites (not ArcadeSprites)
  enemy.body.setBounceX(1);
  enemy.body.setCollideWorldBounds(true);
  enemy.body.velocity.x = 40;
}, this);

 

Edited by samme
changed to `physics.add.group()`

Share this post


Link to post
Share on other sites

Thank you all very much. Hopefully ill get better at spotting Phaser 2 content. I have a feeling i got into this right at a breakingpoint in the migration from 2 to 3.

samme's example was almost the perfect fit. All i needed was to make the group a physics group.

Here is the final working code:

var monsters = this.physics.add.group();

	monsters.addMultiple(
		map.createFromObjects("Monsters", "blob", { key: "monster" })
	);
	monsters.getChildren().forEach(function(enemy) {
		// You need to use the `body` methods because these are Sprites (not ArcadeSprites)

		enemy.body.setBounceX(1);
		enemy.body.setCollideWorldBounds(true);
		enemy.body.velocity.x = 40;
	}, this);
	this.physics.add.collider(groundLayer, monsters);

 

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.