Some Basic Questions about creating different Enemies

Good Day dear Reader,

I am creating new Enemies by triggering a Tile Collision(createEnemies gets called) this works fine but i have 3Types of Enemy and thats wehre i struggle:

how can i set attributes when creating an Enemie? this is how i create Enemies:

this.enemiesObj = this.physics.add.group({
  classType: Enemies
});

createEnemies(){
        this.objectLayer.objects.forEach(obj =>{
            switch(obj.type){
                case "enemies":
                    let dist = Phaser.Math.Distance.BetweenPoints(player,obj)
                    if(dist <= 550){
                        switch(obj.name){
                            case "skelett":
                                this.enemiesObj.get(obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,1,1).setSize(32, 40).setOffset(16, 12);
                                break;
                            case "skelett-stark":
                                this.enemiesObj.get(obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,1,1).setSize(32, 40).setOffset(16, 12);
                                /*
                                  How Can i Set For Example enemie.Health = 50 when creating it?
                                 */
                                break;
                            case "skelett-boss":
                                this.enemiesObj.get(obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,1,1).setSize(32, 40).setOffset(16, 12);
                                /*
                                  How Can i Set For Example enemie.Health = 100 ?
                                 */
                                break;
                        }
                    }    
                break;   
            }
        })
}

Do i need to creat a new Class and Extend the Class Enemy but how…? Man i have no clue :neutral_face:

I am very new to Phaser :slight_smile: but i realy love it!

Thanks for any Help!

NetForce

get() returns the new game object, so you can do whatever with that.

const enemy = this.enemiesObj.get(/*…*/);

enemy.health = 1;

What is Enemies?

1 Like

I found the solution after i posted this Thread by asking ChatGBT :smiley: i had to create the Enemie Object this way:

createEnemies(){
        this.objectLayer.objects.forEach(obj =>{
            switch(obj.type){
                case "enemies":
                    let dist = Phaser.Math.Distance.BetweenPoints(player,obj)
                    if(dist <= 550){
                        switch(obj.name){
                            case "skelett":
                                let enemy = new Enemies(this,obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,"normal");
                                enemy.setSize(32, 40).setOffset(16, 12);
                                this.enemiesObj.add(enemy);
                                break;
                            case "skelett-stark":
                                let enemy2 = new Enemies(this,obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,"stark");
                                enemy2.setSize(32, 40).setOffset(16, 12);
                                this.enemiesObj.add(enemy2);
                                //this.setEnemieTyp(this.enemiesObj,"stark")
                                break;
                            case "skelett-boss":
                                let enemy3 = new Enemies(this,obj.x+obj.width*0.5,obj.y-obj.height*0.5,"skelett",0,"boss");
                                enemy3.setSize(32, 40).setOffset(16, 12);
                                this.enemiesObj.add(enemy3);
                                break;
                        }
                    }    
                break;   
            }
        })
}

my Enemie Class looks like this:

class Enemies extends Phaser.Physics.Arcade.Sprite{
    constructor(scene,x,y,texture,frame,**typ**){
        super(scene,x,y,texture,frame);
        this.scene = scene;
        ...
        **this.typ = typ**

but your solution is better i guess :smiley: thank you for the help
have a good one
NetForce