I’m making a level where a player sprite can catch ‘1-ups’ to increase its health. I want them to fall every 10 seconds. I am recycling the 1-up object, so every time it falls beyond the world’s max y value, its position is reset to the top of the screen.
I am essentially trying to implement the following game logic:
1up spawned -> wait 10 seconds -> fall-> if player catches it or if it falls beyond the max y border, reset to top of the screen -> wait 10 seconds -> fall etc.,
currently this only works for the first few collisions and I suspect that the delayed event keeps getting added, with multiple instances going off at various 10 second intervals, making it look as though the 1-up isn’t waiting to fall at all as all of those delayed events apply to the same object.
Here are some snippets of my code:
    class Level1 extends Phaser.Scene {
	constructor() {
		super("level1");
  }
  xCoords = [64,128,192,256,320,384,448,512,576,640,704,769,833,897,961];
  angles = [90, 0, -90];
  grav = 40;
  score = 0;
  lives = 3;
  timeLeft;
  minutes;
  seconds;
  fallAfterReset;
create() {
//spawning 1ups
    this.oneUp = new OneUp(this, this.xCoords[Math.round(Math.random() * (this.xCoords.length - 1))], 50);
    this.physics.world.enable(this.oneUp);
//waiting 10 seconds after initial spawn:
    this.fallAfterSpawn = this.time.addEvent({
      delay: 10000,
      callback: ()=>{
        this.itemFall(this.oneUp, 30);
      },
      loop: false
  })
this.physics.add.overlap(this.serol, this.oneUp, this.catchOneUp , null, this);
update() {
}
//falling items
  itemFall(item, accel) {
    //set acceleration
    item.body.setAcceleration(0,accel);
    //reset item when it falls beyond the world boundary (top/bottom)
    if (item.y > config.height) {
      this.itemReset(item);
    }
  }
  itemReset(item) {
    item.setVelocityY(0);
    item.body.setAcceleration(0,0);
    item.y = 0;
    item.x = this.xCoords[Math.round(Math.random() * (this.xCoords.length - 1))];
    console.log(item.texture.key);
    if (item.texture.key == "1up") {
      item.setTexture("1up", 0);
      this.fallAfterReset = this.time.addEvent({
        delay: 10000,
        callback: ()=>{
          this.itemFall(item, 30);
        },
        loop: true
    })    
  }
catchOneUp(serol,oneUp){
this.itemReset(oneUp);
//increase life count
if (this.lives >= 3){
  this.lives = 3;
}else{
  this.lives++;
}
this.livesLabel.text = "LIVES " + this.lives;
//update lives gauge
this.lifeGauge.updateLife(this.lives);
  }
  zeroPad(number,size){
    var stringNumber = String(number);
    while(stringNumber.length < (size || 2)){
      stringNumber = "0" + stringNumber;
    }
    return stringNumber;
  }
}
/*1up class*/
class OneUp extends Phaser.Physics.Arcade.Sprite {
  constructor(scene, x=0, y=0, texture = '1up', frame = 0) {
    super(scene,x,y,texture,frame)
    scene.add.existing(this)
    scene.events.on('update', this.update, this)
  }
}
I would really appreciate some help here, as I’ve been stuck in a loop of logic errors for a while… pls :)