I'm writing a small game in Javascript (using just PNGs for graphics and DOM manipulation to move them), and most of the content is procedurally generated. This includes all of the weapons involved, and this is where the problem lies. The problem lies somewhere in here, I believe:
[code=javascript]function Weapon(weaponNa me, weaponDamage, weaponAccuracy, weaponAmmo, weaponRate, weaponClip) {
/* === Weapon Structure ===
* weapon.nm - The weapon's name
* weapon.dmg - Damage per shot
* weapon.acc - Accuracy
* weapon.ammo - Ammo per clip (0 = infinite)
* weapon.rate - Fire rate (rounds per trigger pull)
* weapon.clip - Clips with the weapon (0 = infinite)
*/
this.nm = weaponName;
this.dmg = weaponDamage;
this.acc = weaponAccuracy;
this.ammo = weaponAmmo;
this.rate = weaponRate;
this.clip = weaponClip;
}
// ...
var weapons = []; // Weapon list
var numofweapons = 0;
// ...
cheddar["create_weapons "] = function() {
numofweapons = (Math.floor(3*M ath.random())) + 2; // 2-5 weapons
for (i = 0; i < numofweapons; i++) {
var wname = cheddar.create_ weaponNames(); // Name
// Damage, Accuracy, Ammo, & Fire Rate
weapclass = Math.floor(6*Ma th.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
if (weapclass == 0 || weapclass == 1) {
// LIGHT WEAPON: Between 1% and 20% health taken per shot
// Accurate (99% to 75%) with 5-12 shots per clip, shooting 1-3 at a time
var wdmg = (Math.floor(0.1 9*Math.random() )+0.01).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.2 4*Math.random() )+0.75).toNeare st(0.01);
var wammo = (Math.floor(7*M ath.random()))+ 5;
var firerate = Math.floor(4*Ma th.random());
if (firerate == 4) {
// 1 shot
var wrate = 1;
} else {
var wrate = firerate;
}
} else if (weapclass == 2 || weapclass == 3 || weapclass == 4) {
// MEDIUM WEAPON: Between 21% and 50% damage
// Mid-Ranged (25% to 75%) with 10-30 shots per clip, firing 1-5 at a time
var wdmg = (Math.floor(0.2 9*Math.random() )+0.21).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.5 *Math.random()) +0.25).toNeares t(0.01);
var wammo = Math.floor(20*M ath.random())+1 0;
var wrate = Math.floor(4*Ma th.random())+1;
} else {
// HEAVY WEAPON: 51% - 100%
// Inaccurate (5% - 40%), with 1-100 shots per clip, firing 1-10 at a time
var wdmg = (Math.floor(0.4 9*Math.random() )+0.51).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.3 9*Math.random() )+0.05).toNeare st(0.01);
var wammo = Math.floor(99*M ath.random())+1 ;
var wrate = Math.floor(9*Ma th.random())+1;
}
// Clips
var wclips = Math.floor(5*Ma th.random())+1; // 1-6 clips
var weapons[i] = new Weapon(wname, wdmg, wacc, wammo, wrate, wclips);
}
}[/code]
Every time I call on the weapons array, however, it returns as UNDEFINED. For example, running:
[code=javascript] for (i = 0; i < numofweapons; i++) {
if (weapons[i]) {
alert("weapons["+i+"] exists!");
} else {
alert("weapons["+i+"] does not exist!");
}
}[/code]
pops up the second alert numofweapons times. Can anybody spot the error? I certainly can't...
[code=javascript]function Weapon(weaponNa me, weaponDamage, weaponAccuracy, weaponAmmo, weaponRate, weaponClip) {
/* === Weapon Structure ===
* weapon.nm - The weapon's name
* weapon.dmg - Damage per shot
* weapon.acc - Accuracy
* weapon.ammo - Ammo per clip (0 = infinite)
* weapon.rate - Fire rate (rounds per trigger pull)
* weapon.clip - Clips with the weapon (0 = infinite)
*/
this.nm = weaponName;
this.dmg = weaponDamage;
this.acc = weaponAccuracy;
this.ammo = weaponAmmo;
this.rate = weaponRate;
this.clip = weaponClip;
}
// ...
var weapons = []; // Weapon list
var numofweapons = 0;
// ...
cheddar["create_weapons "] = function() {
numofweapons = (Math.floor(3*M ath.random())) + 2; // 2-5 weapons
for (i = 0; i < numofweapons; i++) {
var wname = cheddar.create_ weaponNames(); // Name
// Damage, Accuracy, Ammo, & Fire Rate
weapclass = Math.floor(6*Ma th.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
if (weapclass == 0 || weapclass == 1) {
// LIGHT WEAPON: Between 1% and 20% health taken per shot
// Accurate (99% to 75%) with 5-12 shots per clip, shooting 1-3 at a time
var wdmg = (Math.floor(0.1 9*Math.random() )+0.01).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.2 4*Math.random() )+0.75).toNeare st(0.01);
var wammo = (Math.floor(7*M ath.random()))+ 5;
var firerate = Math.floor(4*Ma th.random());
if (firerate == 4) {
// 1 shot
var wrate = 1;
} else {
var wrate = firerate;
}
} else if (weapclass == 2 || weapclass == 3 || weapclass == 4) {
// MEDIUM WEAPON: Between 21% and 50% damage
// Mid-Ranged (25% to 75%) with 10-30 shots per clip, firing 1-5 at a time
var wdmg = (Math.floor(0.2 9*Math.random() )+0.21).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.5 *Math.random()) +0.25).toNeares t(0.01);
var wammo = Math.floor(20*M ath.random())+1 0;
var wrate = Math.floor(4*Ma th.random())+1;
} else {
// HEAVY WEAPON: 51% - 100%
// Inaccurate (5% - 40%), with 1-100 shots per clip, firing 1-10 at a time
var wdmg = (Math.floor(0.4 9*Math.random() )+0.51).toNeare st(0.01)*max_he alth;
var wacc = (Math.floor(0.3 9*Math.random() )+0.05).toNeare st(0.01);
var wammo = Math.floor(99*M ath.random())+1 ;
var wrate = Math.floor(9*Ma th.random())+1;
}
// Clips
var wclips = Math.floor(5*Ma th.random())+1; // 1-6 clips
var weapons[i] = new Weapon(wname, wdmg, wacc, wammo, wrate, wclips);
}
}[/code]
Every time I call on the weapons array, however, it returns as UNDEFINED. For example, running:
[code=javascript] for (i = 0; i < numofweapons; i++) {
if (weapons[i]) {
alert("weapons["+i+"] exists!");
} else {
alert("weapons["+i+"] does not exist!");
}
}[/code]
pops up the second alert numofweapons times. Can anybody spot the error? I certainly can't...
Comment