473,801 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defined Array Members List As "Undefined"

17 New Member
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 1533
r035198x
13,262 MVP
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 Recognized Expert Moderator Expert
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 New Member
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 MVP
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
3091
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 across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
1
3488
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 exist eg. First time through loop would display Ford Vauxhall
25
3104
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 don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
49
14532
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
9
10750
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 as a valid return value for the typeof operator in a later version of ECMAScript or is this a JScript "feature"? -- Klaus Johannes Rusch
26
2196
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 perfectly.)
5
10812
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 type="text/javascript"> <!-- var theForm = document.forms;
3
4234
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 instance: test.html: -------------- <html> <head> <script>
0
10520
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10293
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10269
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9102
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7591
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5617
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2960
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.