2015-01-09 34 views
0

我認爲有一個更好的方法,然後重複每個人一遍又一遍的相同的過程?更好的方式來繼續而不是重複

和碰撞方法,碰撞方法只適用於中心到中心的碰撞,這是非常好的。

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

做功能?將值傳遞給對象。 – epascarello

+2

數組和循環? – Musa

回答

1

由於您對每個攻擊者都做了同樣的事情,所以最好製作一個執行該操作的函數,並針對每個攻擊者運行該函數。在這裏,我將把所有攻擊者放在一個數組中,並使用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 ; 
} 
+0

如果'ereset'接受'攻擊者'作爲參數,這個解決方案會更好。自由度越小 - 越好。 – zerkms

+0

爲什麼我沒有想到...謝謝:) –

+1

@zerkms我決定不這樣做,因爲'ereset()'看起來像'play()'或'stop()'這樣的控制方法,可以從那些不一定能夠訪問攻擊者列表的地方。如果重置攻擊者比移動它們更復雜,我會考慮製作另一個名爲'resetAttackers()'的函數,將該數組作爲參數。 –

0

OOP工作:

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(); 
     } 
     } 
}; 

或這樣的事情,這不是測試的代碼,只是一種方法!

+0

感謝您演示OO通常與程序性/功能性相比如何膨脹:-) PS:最好使用構造函數而不是'init' – zerkms

+0

「膨脹」 - 這是完全通用且易於擴展的代碼。那麼爲什麼我們都應該有OOP呢?你可以用fortran77,這是純粹的功能,並很高興。 –

+0

構造函數 - 由於名稱「ereset」,我認爲這只是一個函數,而不是創建它們。語義...... –

相關問題