Heya! I’ve got some prototype inheritance going, but when I initialize instance variables, it looks like the ‘bonus’ variable gets set on the parent, instead of the instance. Help?
The code:
[code]
function WeaponItem( basename, bonus, bonus2, finessed, fx )
{
// first get base item info from tables
// make sure the specified weapon is listed
if ( baseweapons[basename] == void 0 )
{
alert( basename + ‘is not listed in the base weapons’ );
return void 0;
}
// since it is listed, go ahead and get its info, into local var just for typing ease
var bw = baseweapons[basename];
// assorted variable set straight from info, which seem to work fine, ie:
// this.x = bw[y];
// then add any bonuses/specials/etc
// if there’s a weapon bonus, add it
while( bonus || bonus2 )
{
bonus = typeof bonus == ‘number’ ? bonus : 0;
bonus2 = typeof bonus2 == ‘number’ ? bonus2 : 0;
// if total item bonus would go over 10, don’t change anything
if ( bonus + bonus2 > 10 )
{
alert( ‘total bonus for item would exceed 10, no new bonus applied.’ );
break;
}
// the problem must be in here somewhere, as near as I can tell.. but I’m not seeing the mistake??
// if the first bonus isn’t in the acceptable range for nominal bonus, don’t change it
if ( bonus < 6 && bonus >= 0 ) { this.bonus[0] = bonus; }
// ditto second bonus
if ( bonus2 < 6 && bonus2 >= 0 ) { this.bonus[2] = bonus2; }
break;
}
// calcvalue() is an inherited function: it appears to work fine
// recalculate value
this.value = this.calcvalue();
}
// and set the prototype object
WeaponItem.prototype = new WeaponProtoItem();
Any help with this would be deeply appreciated.
cct