Spawning copies of a sprite from a class function

Hi, I have been trying to create a simple game where you can run over zombies with a car, I want the zombies to spawn off the screen and then move down into the screen and be able to be collided with and destroyed by the car. I have been attempting to test enabling and disabling the collider using keys but I cannot figure out how to detect a collision. I need to be able to keep a track of how many of these sprites have been destroyed in a variable, as well as have multiple copies of it on the scene at once. What is the best way to go about this. Here is some of my code, I won’t post it all as its split across multiple files and classes.

This is my main file where I create and update objects. Inside the update function is where I want to call the function from my Zombie class in order to spawn new zombies.

class zombieDriver extends Phaser.Scene {


        //Create Backgorund
        background = this.background = this.add.tileSprite(0, 0, config.width, config.height, "background");

        //Create vehicles physics group
        vehicles =;

        //Create Main player car
        player1 = this.physics.add.existing(new Player(this, config.width/2, config.height/2, 'car'));
        player1.depth = 100;// set depth so car is on top

        //Create Cursors input detection
        cursors = this.input.keyboard.createCursorKeys();
        keyC = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.C);
        keyX = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);

        //Create objects physics group
        objects =;

        //Create Zombie instance
        zombie = this.physics.add.existing(new Zombie(this, config.width/2, config.height/2 - 100, 'zombie'));
        zombie.depth = 99; // set the depth so zombie is beneath car
    update() {
        //Animate Background
        background.tilePositionY -= BACKGROUNDSPEED;
        //Call update player function from player class


Then this is my Zombie class.

class Zombie extends Phaser.Physics.Arcade.Sprite{
    constructor(scene, x, y, texture) {
        super(scene, x, y, texture);
    spawnZombies(zombie, keyC){
            //zombie.checkCollision = true;

At the moment I can only enable and disable the zombie I have created already in the main create function, however I want to be able to create new instances of the zombie from within the zombie class, specifically my spawnZomibes function.

So basically I’m asking how to spawn the zombie from the function in the Zombie class. I can then figure out how to repeatedly spawn them off-screen and move them down for the player to hit and rack up a score.

I would do the zombie spawning in the scene (or possibly a group class, if you want to make one). It doesn’t really make sense in Zombie, since that’s a zombie already existing. And you’ll need a collider. Make sure the car is moving with velocity.

class zombieDriver extends Phaser.Scene {
  // …
  create () {
    // …
    this.physics.add.collider(player1, objects);

  update () {
    // …
    if (keyC.isDown) {
      this.spawnZombie(0, 0);

  spawnZombie (x, y) {
    return objects.add(new Zombie(this, x, y, 'zombie'));

Perfect thank you, just got back to this project after a data loss, am almost back to where it was when i made this post. Just gonna re-draw my zombie sprite and give this a go now.

Just implemented this and it has worked great. Was wondering what i would do if i wanted to detect a collision with each zombie and add to a score counter? @samme

Usually a player vs. zombie overlap collider that destroys the zombie, increments a score variable, and updates a text game object.

Hi @samme , i’ve had a super productive 2 days and i’ve actually finished the game now. Thanks for the help