Defined Array Members List As "Undefined"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kidko
    New Member
    • Apr 2007
    • 17

    Defined Array Members List As "Undefined"

    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...
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      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

      Comment

      • kidko
        New Member
        • Apr 2007
        • 17

        #4
        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:
        [code=Javascript]
        for (var 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) {
        //...
        [/code]
        Just changing it from for (i = 0... to for (var i = 0... fixed it.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by kidko
          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:
          [code=Javascript]
          for (var 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) {
          //...
          [/code]
          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.

          Comment

          Working...