Javascript 从健康中扣除
我有一个船体和护盾,我想在受损时先从护盾中扣除,然后当护盾为空时再从船体中扣除。但如果护盾还剩100点,伤害是400点,那么船体如果从1000点开始就会有700点 在这里,我设法做到了,盾牌部分工作,但船体算法太难,我掌握Javascript 从健康中扣除,javascript,canvas,Javascript,Canvas,我有一个船体和护盾,我想在受损时先从护盾中扣除,然后当护盾为空时再从船体中扣除。但如果护盾还剩100点,伤害是400点,那么船体如果从1000点开始就会有700点 在这里,我设法做到了,盾牌部分工作,但船体算法太难,我掌握 Player.prototype.deductHealth = function(damage) { var shield = (this.PlayerShield - damage); var remainder = (damage - this.PlayerSh
Player.prototype.deductHealth = function(damage)
{
var shield = (this.PlayerShield - damage);
var remainder = (damage - this.PlayerShield);
var hull = this.PlayerHull;
if(this.PlayerShield < 0)
{
hull = (this.PlayerHull - remainder);
}
if(hull <=0)
{
hull = 0;
shield = 0;
}
this.PlayerHull = hull;
this.PlayerShield = shield;
}
Player.prototype.debutcthealth=功能(伤害)
{
var shield=(this.PlayerShield-伤害);
var剩余=(伤害-本玩家场);
var hull=this.PlayerHull;
如果(此.playershild<0)
{
赫尔=(this.PlayerHull-余数);
}
如果(hull我现在不能测试它,但是像这样的东西我认为可以工作
Player.prototype.deductHealth = function(damage)
{
var shield = (this.PlayerShield - damage);
var remainder = 0;
if (shield<0) {
remainder=-shield;
shield=0;
}
var hull = this.PlayerHull;
if(remainder > 0)
{
hull = (this.PlayerHull - remainder);
}
if(hull <=0)
{
hull = 0;
shield = 0;
}
this.PlayerHull = hull;
this.PlayerShield = shield;
}
Player.prototype.debutcthealth=功能(伤害)
{
var shield=(this.PlayerShield-伤害);
var余数=0;
如果(屏蔽0)
{
赫尔=(this.PlayerHull-余数);
}
如果(hullHm…试试这个,我认为您有问题,因为您混淆地引用了表示hull和shield强度的本地变量,以及表示hull和shield的成员变量。我的建议是仅使用成员变量,如下所示:
Player.prototype.deductHealth = function(damage)
{
var shieldOverkill = (damage - this.PlayerShield);
this.PlayerShield = this.PlayerShield - damage;
if ( shieldOverkill > 0 )
{
this.PlayerHull = this.PlayerHull - shieldOverkill;
}
if( this.PlayerHull < 0)
{
this.PlayerHull= 0;
}
if ( this.PlayerShield < 0 )
{
this.PlayerShield = 0;
}
Player.prototype.debutcthealth=功能(伤害)
{
var shieldOverkill=(伤害-本玩家场);
this.playershild=this.playershild-损坏;
如果(防护罩溢流>0)
{
this.PlayerHull=this.PlayerHull-防护罩过度杀伤;
}
如果(此.PlayerHull<0)
{
这个.PlayerHull=0;
}
如果(此.playershild<0)
{
这个.PlayerShield=0;
}
}让我们一步一步地了解它:
Player.protoype.deductHealth = function (damage) {
this.PlayerShield -= damage; // First deduct the damage from the shields:
if (this.PlayerShield < 0) { // If shields are negative
// Take the damage not absorbed by shields from hull;
this.PlayerHull += this.PlayerShield; // note we use + since shields are negative
this.PlayerShield = 0; // Set shields to 0
if (this.PlayerHull < 0) { // If hull now negative
this.PlayerHull = 0; // set to zero.
}
}
}
Player.prototype.debutcthealth=功能(伤害){
this.playershild-=伤害;//首先从护盾中扣除伤害:
如果(this.playershild<0){//如果屏蔽为负
//将防护罩未吸收的损伤从船体上取下;
this.PlayerHull+=this.PlayerShield;//注意我们使用+因为屏蔽是负的
this.PlayerShield=0;//将屏蔽设置为0
如果(this.PlayerHull<0){//如果外壳现在为负
this.PlayerHull=0;//设置为零。
}
}
}
更高级的版本,使用更合适的名称:
Player.prototype.takeHit (damage) {
if ((this.shields -= damage) < 0) { // Shields exhausted ?
if ((this.hull += this.shields) < 0) { // Hull exhausted?
this.hull = 0;
}
this.shields = 0;
}
}
Player.prototype.takeHit(伤害){
如果((this.shields-=损坏)<0){//shields耗尽?
如果((this.hull+=this.shields)<0){//hull耗尽?
这是0.hull=0;
}
这个值为0;
}
}
如果你是EVE的模特,我认为这个问题更适合,那么请注意,护盾上的任何损伤超龄都会被忽略,除了流血,这取决于技能和装备。