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: - function Weapon(weaponName, 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*Math.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*Math.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.19*Math.random())+0.01).toNearest(0.01)*max_health;
-
var wacc = (Math.floor(0.24*Math.random())+0.75).toNearest(0.01);
-
var wammo = (Math.floor(7*Math.random()))+5;
-
var firerate = Math.floor(4*Math.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.29*Math.random())+0.21).toNearest(0.01)*max_health;
-
var wacc = (Math.floor(0.5*Math.random())+0.25).toNearest(0.01);
-
var wammo = Math.floor(20*Math.random())+10;
-
var wrate = Math.floor(4*Math.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.49*Math.random())+0.51).toNearest(0.01)*max_health;
-
var wacc = (Math.floor(0.39*Math.random())+0.05).toNearest(0.01);
-
var wammo = Math.floor(99*Math.random())+1;
-
var wrate = Math.floor(9*Math.random())+1;
-
}
-
-
// Clips
-
var wclips = Math.floor(5*Math.random())+1; // 1-6 clips
-
var weapons[i] = new Weapon(wname, wdmg, wacc, wammo, wrate, wclips);
-
}
-
}
Every time I call on the weapons array, however, it returns as UNDEFINED. For example, running: - for (i = 0; i < numofweapons; i++) {
-
if (weapons[i]) {
-
alert("weapons["+i+"] exists!");
-
} else {
-
alert("weapons["+i+"] does not exist!");
-
}
-
}
pops up the second alert numofweapons times. Can anybody spot the error? I certainly can't...
4 1513
Try removing the var on line 63. You seem to be creating a new weapon[] again each time inside the loop. That array is not available outside the function/loop.
gits 5,390
Expert Mod 4TB
and in case you have extended the native JavaScript-Array (by yourself or using any library like prototype or jquery etc.) ... you should avoid to use the for-in loop for arrays ... use the for (var i = 0; i < n; i++) - loop then ...
kind regards
Well, thanks guys! I removed the var before weapons[i], and that didn't fix it. After a bit more messing around, though, I tried this and it works fine now: -
for (var i = 0; i <= numofweapons; i++) {
-
var wname = cheddar.create_weaponNames(); // Name
-
-
// Damage, Accuracy, Ammo, & Fire Rate
-
weapclass = Math.floor(6*Math.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
-
if (weapclass == 0 || weapclass == 1) {
-
//...
-
Just changing it from for (i = 0... to for (var i = 0... fixed it.
Well, thanks guys! I removed the var before weapons[i], and that didn't fix it. After a bit more messing around, though, I tried this and it works fine now: -
for (var i = 0; i <= numofweapons; i++) {
-
var wname = cheddar.create_weaponNames(); // Name
-
-
// Damage, Accuracy, Ammo, & Fire Rate
-
weapclass = Math.floor(6*Math.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
-
if (weapclass == 0 || weapclass == 1) {
-
//...
-
Just changing it from for (i = 0... to for (var i = 0... fixed it.
That's good news.
Try it again with the var before the weapons[] declaraction and see what happens.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Don Vaillancourt |
last post by:
What's going on with Javascript.
At the beginning there was the "undefined" value which represented an
object which really didn't exist then came the null keyword. But
yesterday I stumbled...
|
by: Chris |
last post by:
How do I stop displaying "undefined" when using the code below.
The problem occurs as I want to display 5 items per page using
setInterval but this causes 'mycars' to reach a value that doesn't...
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I...
|
by: matty |
last post by:
Hi,
I recently got very confused (well that's my life) about the
"undefined" value. I looked in the FAQ and didn't see anything about
it. On...
|
by: Klaus Johannes Rusch |
last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of
an unknown property of an object, for example
document.write(typeof window.missingproperty);
Has "unknown" been defined...
|
by: Frederick Gotham |
last post by:
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:
(1) Well-defined behaviour:
int a = 2, b = 3;
int c = a + b;
(Jist: The code will work...
|
by: Nathan Sokalski |
last post by:
I have an ASP.NET application which is giving the following JavaScript
error:
'theForm' is undefined
However, when I do a View Source one of the <scriptelements is as follows:
<script...
|
by: gsuns82 |
last post by:
HI all,
I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file.
For an...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |