Skip to main content
fixed typos & code formatting
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

How can I delete an object when itsit's not stored in an array?

So i have this problem where iI have a boss appearing in my game  , but since itsit's only one character iI didn't put it in an array , and now im. Now I'm trying to make disappear if its health is less or equal to 0 to dissapear but iI don't know how to do that , is. Is there a way to remove the object  ? Because iI only know how to remove a property of an object. Thats

Here's the boss object:

    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function () {
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}

 
//enemy wave object




 function random(min, max) {
        return Math.random() * (max - min) + min; 
}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show is= whenfunction(){
 the boss is spawning    this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
        this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
        this.x, this.y, this.frameWidth, this.frameHeight );
    }
    this.move = function(){
        this.x = this.x - this.dx;
    }
    this.deletion = function(){
        this.toDelete = true;
    }
}

//this is when the boss is spawning
//the game loop
function draw() {
        bullets = bullets.filter((bullet) => bullet.x <= viewLimit);
        enemies = enemies.filter((enemy) => enemy.x >= viewLimit1);
        while (enemies.length <= enemiesPerTick && kills <= 5) {
            var enemy = new Enemy(canvas.width, random(0, canvas.height - 
            60));
            enemies.push(enemy);
        }
        for (var v = 0; v < enemies.length; v++) {
            enemies[v].showHealth();
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        hero.show();
        hero.update();
        hero.showHealth();
        for (var i = 0; i < bullets.length; i++) {
            bullets[i].show();
            bullets[i].move();
        }
        for (var j = 0; j < enemies.length; j++) {
            enemies[j].show();
            enemies[j].move();
        }
            for (var t = 0; t < enemies.length; t++) {
                enemies[t].showHealth();
            }
            collision();
            drawScore();
            if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }

            requestAnimationFrame(draw);
        }
         
console.log(enemies, bullets);
        draw();

How can I delete an object when its not stored in an array

So i have this problem where i have a boss appearing in my game  , but since its only one character i didn't put it in an array , and now im trying to make if its health is less or equal to 0 to dissapear but i don't know how to do that , is there a way to remove the object  ? Because i only know how to remove a property of an object. Thats the boss object:

    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}

 
//enemy wave object




 function random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning

//the game loop
function draw() {
        bullets = bullets.filter((bullet) => bullet.x <= viewLimit);
        enemies = enemies.filter((enemy) => enemy.x >= viewLimit1);
        while (enemies.length <= enemiesPerTick && kills <= 5) {
            var enemy = new Enemy(canvas.width, random(0, canvas.height - 
            60));
            enemies.push(enemy);
        }
        for (var v = 0; v < enemies.length; v++) {
            enemies[v].showHealth();
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        hero.show();
        hero.update();
        hero.showHealth();
        for (var i = 0; i < bullets.length; i++) {
            bullets[i].show();
            bullets[i].move();
        }
        for (var j = 0; j < enemies.length; j++) {
            enemies[j].show();
            enemies[j].move();
        }
            for (var t = 0; t < enemies.length; t++) {
                enemies[t].showHealth();
            }
            collision();
            drawScore();
            if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }

            requestAnimationFrame(draw);
        }
        console.log(enemies, bullets);
        draw();

How can I delete an object when it's not stored in an array?

I have a boss appearing in my game, but since it's only one character I didn't put it in an array. Now I'm trying to make disappear if its health is less or equal to 0 but I don't know how to do that. Is there a way to remove the object? I only know how to remove a property of an object.

Here's the boss object:

function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function () {
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}

//enemy wave object
function random(min, max) {
    return Math.random() * (max - min) + min; 
}
function Enemy(x, y){
    this.sprite = enemySprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 128;
    this.imgHeight = 192;
    this.frameWidth = this.imgWidth / 4 ;
    this.frameHeight = this.imgHeight / 4;
    this.toDelete = false;
    this.dx = random(1, 3);
    this.enemiesHealth = 100;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.updateFrame = function(){
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function(){
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
        this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
        this.x, this.y, this.frameWidth, this.frameHeight );
    }
    this.move = function(){
        this.x = this.x - this.dx;
    }
    this.deletion = function(){
        this.toDelete = true;
    }
}

//this is when the boss is spawning
//the game loop
function draw() {
    bullets = bullets.filter((bullet) => bullet.x <= viewLimit);
    enemies = enemies.filter((enemy) => enemy.x >= viewLimit1);
    while (enemies.length <= enemiesPerTick && kills <= 5) {
        var enemy = new Enemy(canvas.width, random(0, canvas.height - 60));
        enemies.push(enemy);
    }
    for (var v = 0; v < enemies.length; v++) {
        enemies[v].showHealth();
    }
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    hero.show();
    hero.update();
    hero.showHealth();
    for (var i = 0; i < bullets.length; i++) {
        bullets[i].show();
        bullets[i].move();
    }
    for (var j = 0; j < enemies.length; j++) {
        enemies[j].show();
        enemies[j].move();
    }
    for (var t = 0; t < enemies.length; t++) {
        enemies[t].showHealth();
    }
    collision();
    drawScore();
    if (enemies.length == 0) {
        boss1.show();
        boss1.move();
        boss1.showHealth();
    }

    requestAnimationFrame(draw);
}
 
console.log(enemies, bullets);
draw();
added 1289 characters in body
Source Link
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}


//enemy wave object


    f

unctionfunction random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning

//the game loop
function draw() {
        bullets = bullets.filter((bullet) => bullet.x <= viewLimit);
        enemies = enemies.filter((enemy) => enemy.x >= viewLimit1);
        while (enemies.length <= enemiesPerTick && kills <= 5) {
            var enemy = new Enemy(canvas.width, random(0, canvas.height - 
            60));
            enemies.push(enemy);
        }
        for (var v = 0; v < enemies.length; v++) {
            enemies[v].showHealth();
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        hero.show();
        hero.update();
        hero.showHealth();
        for (var i = 0; i < bullets.length; i++) {
            bullets[i].show();
            bullets[i].move();
        }
        for (var j = 0; j < enemies.length; j++) {
            enemies[j].show();
            enemies[j].move();
        }
            for (var t = 0; t < enemies.length; t++) {
                enemies[t].showHealth();
            }
            collision();
            drawScore();
            if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }

            requestAnimationFrame(draw);
        }
        console.log(enemies, bullets);
        draw();
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}


//enemy wave object


    f

unction random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning
  if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}


//enemy wave object




function random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning

//the game loop
function draw() {
        bullets = bullets.filter((bullet) => bullet.x <= viewLimit);
        enemies = enemies.filter((enemy) => enemy.x >= viewLimit1);
        while (enemies.length <= enemiesPerTick && kills <= 5) {
            var enemy = new Enemy(canvas.width, random(0, canvas.height - 
            60));
            enemies.push(enemy);
        }
        for (var v = 0; v < enemies.length; v++) {
            enemies[v].showHealth();
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        hero.show();
        hero.update();
        hero.showHealth();
        for (var i = 0; i < bullets.length; i++) {
            bullets[i].show();
            bullets[i].move();
        }
        for (var j = 0; j < enemies.length; j++) {
            enemies[j].show();
            enemies[j].move();
        }
            for (var t = 0; t < enemies.length; t++) {
                enemies[t].showHealth();
            }
            collision();
            drawScore();
            if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }

            requestAnimationFrame(draw);
        }
        console.log(enemies, bullets);
        draw();
added 1757 characters in body
Source Link
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
} 


//enemy wave object


    f

unction random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning
  if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
}
    function Boss1(x, y) {
    this.sprite = boss1Sprite;
    this.x = x;
    this.y = y;
    this.imgWidth = 160;
    this.imgHeight = 224;
    this.frameWidth = this.imgWidth / 4;
    this.frameHeight = this.imgHeight / 4;
    this.health = 1500;
    this.directionMode = 1;
    this.currentFrame = 0;
    this.SPEED = 1;
    this.delta = 1;
    this.updateFrame = function () {
        this.currentFrame += 0.2;
    }
    this.showHealth = function (){
        ctx.fillStyle="red";
        ctx.fillRect(this.x,this.y - 10,(this.health/100)*3.3,5);
        ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
    }
    this.show = function () {
        this.updateFrame();
        this.walkingMode = Math.floor(this.currentFrame) % 4;
        ctx.drawImage(this.sprite, this.walkingMode * this.frameWidth, this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, this.x, this.y, this.frameWidth, this.frameHeight);
    }
    //target == hero
    //position == this
    this.move = function () {
        this.dx = hero.x - this.x;
        this.dy = hero.y - this.y;
        this.length = Math.sqrt(this.dx * this.dx + this.dy * this.dy);
        if (this.length) {
            this.dx = this.dx / this.length;
            this.dy = this.dy / this.length;
        }
        this.x += this.dx * this.delta * this.SPEED;
        this.y += this.dy * this.delta * this.SPEED;
    }
} 


//enemy wave object


    f

unction random(min, max) {
        return Math.random() * (max - min) + min;}
    function Enemy(x, y){
        this.sprite = enemySprite;
        this.x = x;
        this.y = y;
        this.imgWidth = 128;
        this.imgHeight = 192;
        this.frameWidth = this.imgWidth / 4 ;
        this.frameHeight = this.imgHeight / 4;
        this.toDelete = false;
        this.dx = random(1, 3);
        this.enemiesHealth = 100;
        this.directionMode = 1;
        this.currentFrame = 0;
        this.updateFrame = function(){
            this.currentFrame += 0.2;
        }
        this.showHealth = function (){
            ctx.fillStyle="red";
            ctx.fillRect(this.x,this.y - 10,(this.enemiesHealth/100)*50,5);
            ctx.strokeRect(this.x, this.y-10, (100/100) * 50, 5);
        }
        this.show = function(){
            this.updateFrame();
            this.walkingMode = Math.floor(this.currentFrame) % 4;
            ctx.drawImage(this.sprite,this.walkingMode * this.frameWidth, 
   this.directionMode * this.frameHeight, this.frameWidth, this.frameHeight, 
  this.x, this.y, this.frameWidth, this.frameHeight );
        }
        this.move = function(){
            this.x = this.x - this.dx;
        }
        this.deletion = function(){
            this.toDelete = true;
        }
    }

  //this is when the boss is spawning
  if (enemies.length == 0) {
                boss1.show();
                boss1.move();
                boss1.showHealth();
            }
Source Link
Loading