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 1533
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
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
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 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
|
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
|
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 ??
|
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.
|
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
| |
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.)
|
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;
|
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>
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
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...
| |