Jump to content

spawning the same enemy in various points using tiled


balrog_
 Share

Recommended Posts

Hello, I'm new with phaser and I'm building a platform game.

I'm using tiled to create my map, I created an object Layer to place the player and the same enemy in specific points of the map, I did the enemies with this:

var enemies = this.game.add.group();

this.map.createFromObjects('objectsLayer', 81, 'zombie', 0, true, false, this.enemies);


They are showing on the map correctly but I don't know how to give them gravity, change their hitbox, animations, etc. They are just sprites floating there with no life.

I want to give an enemy all the configuration and then make it affect all the enemies, also put them in a group.

If there is another way please tell me.

I was using this tutorial https://gamedevacademy.org/html5-phaser-tutorial-top-down-games-with-tiled/

sorry for my english.

 

Link to comment
Share on other sites

Depending on what type of physics you're using, it will pay off to use a physics group for your enemies group.  This will automatically apply physics configurations to each of the sprite.

If you're doing more customized physics than basic things supported by whatever system you're using (world gravity, bounce, ect), you'll need to do a enemies.forEach() to apply these physics properties.

 

Link to comment
Share on other sites

I want to give them gravity, hitbox size, anchor position, animations... and also give them some AI.

I tried a code from this post http://www.html5gamedevs.com/topic/5746-placing-enemies-on-a-tilemap/#comment-34699,

as in the post says. this.enemy.name = the name given to the object in Tiled, so every object that has zombie as his name will have this configs

but it returns: Game.js:105 Uncaught TypeError: Cannot read property 'name' of undefined

var enemies;
this.enemies = this.game.add.group();

    this.map.createFromObjects('objectsLayer', 81, 'zombie', 0, true, false, this.enemies);
    this.enemies.forEach(setupEnemies, this);

.......

function setupEnemies(enemy){
    if (this.enemy.name == 'zombie'){
      //this.enemy.scale.setTo(0.6,0.6);
      this.enemy.animations.add('iddle', [ 0], 1, true);
      this.enemy.animations.play('iddle');
      this.enemy.body.setSize(4, 14, 0, 1);
      this.enemy.health=10;
      this.enemy.body.gravity.y = player_gravtyForce;
     }
   }
 },

 

Link to comment
Share on other sites

Hi,

 

you are confusing some things here.

Your obvious problem in the code is:

You are using "this.enemy" instead of just "enemy" in your function.

There is no "this.enemy" in your code, just the parameter "enemy" that you should use.

This should get you going.

Next thing you should check out is "how to extend phaser sprite".

This will allow you to build complex objects like ai driven enemies much easier.

Link to comment
Share on other sites

Hello, thanks for your help, I did that and worked, I put "this" because I was modifying the code to make it work, and also I forgot to put "this.enemies.enable Body = true;".

It let me modify the hitbox, give them gravity, and basic stuff... and also collide with the world, but it seems that it wont let me give them dynamic actions that requires to be updated.

So that means that I have to look about that "extend phaser sprite" method. If you have any tips about that, would be greatly appreciated.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...