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 {
constructor(){
super("mainGame");
}
create(){
//Create Backgorund
background = this.background = this.add.tileSprite(0, 0, config.width, config.height, "background");
background.setOrigin(0,0);
//Create vehicles physics group
vehicles = this.physics.add.group();
//Create Main player car
player1 = this.physics.add.existing(new Player(this, config.width/2, config.height/2, 'car'));
vehicles.add(player1);
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 = this.physics.add.group();
//Create Zombie instance
zombie = this.physics.add.existing(new Zombie(this, config.width/2, config.height/2 - 100, 'zombie'));
objects.add(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
player1.updatePlayer(player1,cursors);
zombie.spawnZombies(zombie,keyC,keyX);
}
}
Then this is my Zombie class.
class Zombie extends Phaser.Physics.Arcade.Sprite{
constructor(scene, x, y, texture) {
super(scene, x, y, texture);
scene.add.existing(this);
}
spawnZombies(zombie, keyC){
if(keyC.isDown){
zombie.setActive(false).setVisible(false);
//zombie.checkCollision = true;
//zombie.destroy();
}
if(keyX.isDown){
zombie.setActive(true).setVisible(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.