473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Defined Array Members List As "Undefined"

17
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:
Expand|Select|Wrap|Line Numbers
  1. function Weapon(weaponName, weaponDamage, weaponAccuracy, weaponAmmo, weaponRate, weaponClip) {
  2.     /* === Weapon Structure ===
  3.      * weapon.nm - The weapon's name
  4.      * weapon.dmg - Damage per shot
  5.      * weapon.acc - Accuracy
  6.      * weapon.ammo - Ammo per clip (0 = infinite)
  7.      * weapon.rate - Fire rate (rounds per trigger pull)
  8.      * weapon.clip - Clips with the weapon (0 = infinite)
  9.      */
  10.     this.nm = weaponName;
  11.     this.dmg = weaponDamage;
  12.     this.acc = weaponAccuracy;
  13.     this.ammo = weaponAmmo;
  14.     this.rate = weaponRate;
  15.     this.clip = weaponClip;
  16. }
  17.  
  18. // ... 
  19.  
  20. var weapons = []; // Weapon list
  21. var numofweapons = 0;
  22.  
  23. // ... 
  24.  
  25. cheddar["create_weapons"] = function() {
  26.     numofweapons = (Math.floor(3*Math.random())) + 2; // 2-5 weapons
  27.     for (i = 0; i < numofweapons; i++) {
  28.         var wname = cheddar.create_weaponNames(); // Name
  29.  
  30.         // Damage, Accuracy, Ammo, & Fire Rate
  31.         weapclass = Math.floor(6*Math.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
  32.         if (weapclass == 0 || weapclass == 1) {
  33.             // LIGHT WEAPON: Between 1% and 20% health taken per shot
  34.             // Accurate (99% to 75%) with 5-12 shots per clip, shooting 1-3 at a time
  35.             var wdmg = (Math.floor(0.19*Math.random())+0.01).toNearest(0.01)*max_health;
  36.             var wacc = (Math.floor(0.24*Math.random())+0.75).toNearest(0.01);
  37.             var wammo = (Math.floor(7*Math.random()))+5;
  38.             var firerate = Math.floor(4*Math.random());
  39.             if (firerate == 4) {
  40.                 // 1 shot
  41.                 var wrate = 1;
  42.             } else {
  43.                 var wrate = firerate;
  44.             }
  45.         } else if (weapclass == 2 || weapclass == 3 || weapclass == 4) {
  46.             // MEDIUM WEAPON: Between 21% and 50% damage
  47.             // Mid-Ranged (25% to 75%) with 10-30 shots per clip, firing 1-5 at a time
  48.             var wdmg = (Math.floor(0.29*Math.random())+0.21).toNearest(0.01)*max_health;
  49.             var wacc = (Math.floor(0.5*Math.random())+0.25).toNearest(0.01);
  50.             var wammo = Math.floor(20*Math.random())+10;
  51.             var wrate = Math.floor(4*Math.random())+1;
  52.         } else {
  53.             // HEAVY WEAPON: 51% - 100%
  54.             // Inaccurate (5% - 40%), with 1-100 shots per clip, firing 1-10 at a time
  55.             var wdmg = (Math.floor(0.49*Math.random())+0.51).toNearest(0.01)*max_health;
  56.             var wacc = (Math.floor(0.39*Math.random())+0.05).toNearest(0.01);
  57.             var wammo = Math.floor(99*Math.random())+1;
  58.             var wrate = Math.floor(9*Math.random())+1;
  59.         }
  60.  
  61.         // Clips
  62.         var wclips = Math.floor(5*Math.random())+1; // 1-6 clips
  63.         var weapons[i] = new Weapon(wname, wdmg, wacc, wammo, wrate, wclips);
  64.     }
  65. }
Every time I call on the weapons array, however, it returns as UNDEFINED. For example, running:
Expand|Select|Wrap|Line Numbers
  1.     for (i = 0; i < numofweapons; i++) {
  2.         if (weapons[i]) {
  3.             alert("weapons["+i+"] exists!");
  4.         } else {
  5.             alert("weapons["+i+"] does not exist!");
  6.         }
  7.     }
pops up the second alert numofweapons times. Can anybody spot the error? I certainly can't...
Jun 4 '08 #1
4 1513
r035198x
13,262 8TB
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.
Jun 5 '08 #2
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
Jun 5 '08 #3
kidko
17
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:
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0; i <= numofweapons; i++) {
  2.         var wname = cheddar.create_weaponNames(); // Name
  3.  
  4.         // Damage, Accuracy, Ammo, & Fire Rate
  5.         weapclass = Math.floor(6*Math.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
  6.         if (weapclass == 0 || weapclass == 1) {
  7. //...
  8.  
Just changing it from for (i = 0... to for (var i = 0... fixed it.
Jun 5 '08 #4
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. for (var i = 0; i <= numofweapons; i++) {
  2.         var wname = cheddar.create_weaponNames(); // Name
  3.  
  4.         // Damage, Accuracy, Ammo, & Fire Rate
  5.         weapclass = Math.floor(6*Math.random()); // Weapon class, 0-1 being light, 2-4 medium, 5 heavy
  6.         if (weapclass == 0 || weapclass == 1) {
  7. //...
  8.  
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.
Jun 6 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
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...
1
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...
25
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...
49
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...
9
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...
26
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...
5
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...
3
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...
0
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,...
0
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$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.