Jursdotme Posted December 4, 2018 Share Posted December 4, 2018 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. Link to comment Share on other sites More sharing options...
Jursdotme Posted December 4, 2018 Author Share Posted December 4, 2018 The complete project can be found here: https://github.com/Jursdotme/game Link to comment Share on other sites More sharing options...
B3L7 Posted December 4, 2018 Share Posted December 4, 2018 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: https://labs.phaser.io/edit.html?src=src\game objects\tilemap\static\create from objects.js https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tilemap.html#createFromObjects__anchor Also, it doesn't look like your repo is public. Jursdotme and samme 2 Link to comment Share on other sites More sharing options...
wclarkson Posted December 5, 2018 Share Posted December 5, 2018 try group.children.getArray().forEach Jursdotme 1 Link to comment Share on other sites More sharing options...
samme Posted December 5, 2018 Share Posted December 5, 2018 (edited) 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 December 6, 2018 by samme changed to `physics.add.group()` Jursdotme and B3L7 1 1 Link to comment Share on other sites More sharing options...
Jursdotme Posted December 6, 2018 Author Share Posted December 6, 2018 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); samme and B3L7 2 Link to comment Share on other sites More sharing options...
Recommended Posts