/    Sign up×
Community /Pin to ProfileBookmark

prototype variables

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();
[/code]

Any help with this would be deeply appreciated.

cct

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@cctauthorSep 17.2007 — Hey again. Just in case the loop within which the object creation is occurring is part of the problem, here's the code:

( a is an array of weapon info rows)
<i>
</i> var a = top.cdata.eqlist.weapons;
for ( var i=0; i&lt;a.length; i++ )
{
// need some things - basename, bonus1, bonus2, equipped, finessed
var newobj = new WeaponItem( a[i][0], a[i][1][0], a[i][1][1], a[i][4], a[i][5] );
top.chardata.inventory.objects.weapons.push( newobj );
}


Hope that helps

cct
×

Success!

Help @cct spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...