Cant see object when cloning in new level

23 Views Asked by At

So currently im having issues with my 2nd level, for some reason i am not able to see my cloned zombie so i can have a total of 3, i might not be cloning the zombie right? Im not sure, If yall can tell me what im doing wrong that would be greatly appreaciated. Im still new learning so anything will useful. Thank you



let currentLevel = 2; // Track the current level

// both zombies are dead
function areZombiesDead() {
  return yellowZombie.isDead && zombieTwo.isDead;
}
// Function to check if all zombies are dead
function areZombiesDeadLevelTwo() {
  return clonedZombieYellow.isDead && yellowZombie.isDead && zombieTwo.isDead;
}



function updateGameState() {
  // Update game logic, such as checking collisions, player input, etc.

  // Check if any zombie is dead in level 1
  if (currentLevel === 1) {
    if (yellowZombie.health <= 0 && !yellowZombie.isDead) {
      yellowZombie.isDead = true;
      // Perform actions when yellow zombie dies
      console.log("Yellow Zombie has died!");
    }

    if (zombieTwo.health <= 0 && !zombieTwo.isDead) {
      zombieTwo.isDead = true;
      // Perform actions when zombieTwo dies
      console.log("Zombie Two has died!");
    }

    // Check if both zombies are dead in level 1
    if (areZombiesDead()) {
      console.log("Both zombies are dead! Level  1 complete!");

      // Transition to level 2
      currentLevel = 2;
      resetLevelTwo(); // Initialize level 2 elements
      console.log('Level 2 starts')

    }
  } else if (currentLevel === 2) {
    if (yellowZombie.health <= 0 && !yellowZombie.isDead) {
      yellowZombie.isDead = true;
      // Perform actions when yellow zombie dies
      console.log("Yellow Zombie level 2 has died!");
    }
    if (zombieTwo.health <= 0 && !zombieTwo.isDead) {
      zombieTwo.isDead = true;
      // Perform actions when zombieTwo dies
      console.log("Zombie Two  level 2 has died!");
    }
    if (clonedZombieYellow.health <= 0 && !clonedZombieYellow.isDead) {
      clonedZombieYellow.isDead = true;
      // Perform actions when zombieTwo dies
      console.log("cloned ZombieYellow  level 2 has died!");
    }
    // Check if both zombies are dead in level 1
    if (areZombiesDeadLevelTwo()) {
      console.log("All zombies are dead! Level  2 complete!");

      // Transition to level 2
      currentLevel = 3;
      console.log('Level 3 starts')

    }
  }
}


// Function to reset game state for level 2
function resetLevelTwo() {
  // Reset game state for level 2

  // Reset yellow zombies
  yellowZombie.health = yellowZombie.maxHealth;
  yellowZombie.isDead = false;
  yellowZombie.x = 100;
  yellowZombie.y = 0;

  // Reset zombieTwo
  zombieTwo.health = zombieTwo.maxHealth;
  zombieTwo.isDead = false;
  zombieTwo.x = 300;
  zombieTwo.y = 0;

// Reset zombieTwo
clonedZombieYellow.health = clonedZombieYellow.maxHealth;
clonedZombieYellow.isDead = false;
clonedZombieYellow.x = 300;
clonedZombieYellow.y = 0;
}

function cloneYellowZombie() {
  const clonedZombieYellow = {
      x: yellowZombie.x,
      y: yellowZombie.y,
      width: yellowZombie.width,
      height: yellowZombie.height,
      velocityY: yellowZombie.velocityY,
      gravity: yellowZombie.gravity,
      normalizedDirectionX: yellowZombie.normalizedDirectionX,
      isAttacking: yellowZombie.isAttacking,
      attackFrames: yellowZombie.attackFrames.slice(), // Create a copy of the attack frames array
      currentAttackFrame: yellowZombie.currentAttackFrame,
      attackDuration: yellowZombie.attackDuration,
      attackSpeed: yellowZombie.attackSpeed,
      health: yellowZombie.health,
      maxHealth: yellowZombie.maxHealth,
      deathFrames: yellowZombie.deathFrames.slice(), // Create a copy of the death frames array
      isJumping: yellowZombie.isJumping,
      jumpVelocity: yellowZombie.jumpVelocity,
      jumpCooldown: yellowZombie.jumpCooldown,
      jumpFrames: yellowZombie.jumpFrames,
      currentJumpFrame: yellowZombie.currentJumpFrame,
      jumpHeight: yellowZombie.jumpHeight,
      isDead: yellowZombie.isDead,
      yellowZombieRun: new Image(), // Create new image objects for animations
      yellowZombieAttack: new Image(),
      yellowZombieDead: new Image(),
      yellowZombieJump: new Image(),
  };

  //  image sources
  clonedZombieYellow.yellowZombieRun.src = yellowZombieRun.src;
  clonedZombieYellow.yellowZombieAttack.src = yellowZombieAttack.src;
  clonedZombieYellow.yellowZombieDead.src = yellowZombieDead.src;
  clonedZombieYellow.yellowZombieJump.src = yellowZombieJump.src;

  return clonedZombieYellow;
}

const clonedZombieYellow = cloneYellowZombie();




function gameLoop() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  drawYellowZombie(clonedZombieYellow); // Use the cloned zombie here
  update();
  cloneYellowZombie();
  moveZombieTwo();
  drawFrame();

  moveYellowZombie();
  checkCollisionWithPlatforms(platforms);
  startYellowZombieAttack();
  drawYellowZombie();

  if (isShotAnimationPlaying) {
    drawShotAnimation();
  } else if (isReloadAnimationPlaying) {
    drawReloadAnimation();
  } else {
    checkCollision();
    drawPlayer();
  }
  updateGameState();
  renderMap();

  

  requestAnimationFrame(gameLoop);
}

Tried to add a spawning system but that was just a headache.

0

There are 0 best solutions below