Лучший способ продолжить вместо повторения

Я предполагаю, что есть лучший способ, чем повторять один и тот же процесс снова и снова для каждого?

и метод столкновения, метод столкновения работает только с межцентровым столкновением, что действительно хорошо.

var ereset = function(){
    //Attacker 1
    att1.x = 0 + (Math.random() * (canvas.width - 64));
    att1.y = -60 ;  
    //Attacker 2
    att2.x = 0 + (Math.random() * (canvas.width - 64));
    att2.y = -60 ;      
    //Attacker 3
    att3.x = 0 + (Math.random() * (canvas.width - 64));
    att3.y = -60 ;  
    //Attacker 4
    att4.x = 0 + (Math.random() * (canvas.width - 64));
    att4.y = -60 ;  
    //Attacker 5
    att5.x = 0 + (Math.random() * (canvas.width - 64));
    att5.y = -60 ;  
}



  if (
        hero.x <= (att1.x + 20 || att1.x + 32)
        && att1.x <= (hero.x + 20 || att1.x + 32)
        && hero.y <= (att1.y + 20 || att1.y - 32)
        && att1.y <= (hero.y + 20 || att1.y - 32)
    ){
        end();
    } else if(
        hero.x <= (att2.x + 20 || att2.x + 32)
        && att2.x <= (hero.x + 20 || att2.x + 32)
        && hero.y <= (att2.y + 20 || att2.y - 32)
        && att2.y <= (hero.y + 20 || att2.y - 32)
    ){
        end();
    }else if(
        hero.x <= (att3.x + 20 || att3.x + 32)
        && att3.x <= (hero.x + 20 || att3.x + 32)
        && hero.y <= (att3.y + 20 || att3.y - 32)
        && att3.y <= (hero.y + 20 || att3.y - 32)
    ){
        end();
    }else if(
        hero.x <= (att4.x + 20 || att4.x + 32)
        && att4.x <= (hero.x + 20 || att4.x + 32)
        && hero.y <= (att4.y + 20 || att4.y - 32)
        && att4.y <= (hero.y + 20 || att4.y - 32)
    ){
        end();
    }

};

2 ответа

Поскольку вы делаете то же самое для каждого злоумышленника, вероятно, лучше всего создать функцию, которая выполняет это действие, и запустить ее против каждого злоумышленника. Здесь я помещу всех атакующих в массив и использую forEach перебрать список.

var attackers = [
    att1, att2, att3, att4, att5
];

function ereset(){
    attackers.forEach(moveAttacker);
}

function moveAttacker(attacker){
    attacker.x = 0 + (Math.random() * (canvas.width - 64));
    attacker.y = -60 ; 
}

ООП Работа:

var Attacker = function() {
    this.x = 0;
    this.y = 0;

    this.init = function(ix, iy) {
      this.x = ix;
      this.y = iy;
    };
};

var ereset = function(){

    this.attackers = 5;
    this.swat = [];

    for ( var n = 0; n < this.attackers; n++ )
    {
         var att = new Attacker();
         att.init(0 + (Math.random() * (canvas.width - 64)), -60 );
         this.swat.push(att);
    }
};

var checkHero = function (hero, attackers)
{
      for ( var n = 0; n < attackers.length; n++  )
      {
         if (
             hero.x <= (attackers[n].x + 20 || attackers[n].x + 32)
             && attackers[n].x <= (hero.x + 20 || attackers[n].x + 32)
             && hero.y <= (att1.y + 20 || attackers[n].y - 32)
             && attackers[n].y <= (hero.y + 20 || attackers[n].y - 32)
            ){
               end();
         }
       }
 };

или что-то вроде этого, это не проверенный код, просто подход!

Другие вопросы по тегам